The following two SQL's error message is the same.
1.
MySQL = "INSERT INTO [Equipment] VALUES('" & Nz(cbGroupCat) & "','" &
Nz(tbEquipNo) & "',""" & Nz(tbEquipDesc) & """,""" & Nz(tbAssetNo) & ""","""
& Nz(tbPartNo) & """,""" & Nz(tbModelNo) & """,""" & Nz(tbSerialNo) &
""",""" & Nz(tbDetails) & """," & Nz(tbOriginCost) & ",'" & Nz(tbLocation) &
"',#" & Nz(tbPurchasedDate) & "#,#" & Nz(tbInstalledDate) & "#,#" &
Nz(tbWarranteeUntil) & "#,""" & Nz(tbTechFile) & """,""" & Nz(tbMFR) &
""",1,#" & Nz(tbOutUntil) & "#,#" & Nz(tbTakenOut) & "#,#" &
Nz(tbLastReading) & "#," & Nz(tbCurrentHours) & "," & Nz(tbCurrentMiles) &
"," & Nz(tbCurrentKM) & ");"
2. Dim MySQL As String
Dim SQLFields As String
Dim SQLValues As String
SQLFields = "("
SQLValues = "VALUES("
If Not IsNull(Me![cbGroupCat]) Then
SQLFields = SQLFields & "GroupCategory, "
SQLValues = SQLValues & "'" & Me![cbGroupCat] & "', "
End If
If Not IsNull(Me![tbEquipNo]) Then
SQLFields = SQLFields & "EquipNo, "
SQLValues = SQLValues & "'" & Me![tbEquipNo] & "', "
End If
If Not IsNull(Me![tbEquipDesc]) Then
SQLFields = SQLFields & "EquipDesc, "
SQLValues = SQLValues & "'" & Me![tbEquipDesc] & "', "
End If
If Not IsNull(Me![tbAssetNo]) Then
SQLFields = SQLFields & "AssetNo, "
SQLValues = SQLValues & "'" & Me![tbAssetNo] & "', "
End If
If Not IsNull(Me![tbPartNo]) Then
SQLFields = SQLFields & "PartNo, "
SQLValues = SQLValues & "'" & Me![tbPartNo] & "', "
End If
If Not IsNull(Me![tbModelNo]) Then
SQLFields = SQLFields & "ModelNo, "
SQLValues = SQLValues & "'" & Me![tbModelNo] & "', "
End If
If Not IsNull(Me![tbSerialNo]) Then
SQLFields = SQLFields & "SerialNo, "
SQLValues = SQLValues & "'" & Me![tbSerialNo] & "', "
End If
If Not IsNull(Me![tbDetails]) Then
SQLFields = SQLFields & "EquipDetails, "
SQLValues = SQLValues & "'" & Me![tbDetails] & "', "
End If
If Not IsNull(Me![tbOriginCost]) Then
SQLFields = SQLFields & "OriginalCost, "
SQLValues = SQLValues & Me![tbOriginCost] & ", "
End If
If Not IsNull(Me![tbLocation]) Then
SQLFields = SQLFields & "Location, "
SQLValues = SQLValues & "'" & Me![tbLocation] & "', "
End If
If Not IsNull(Me![tbPurchasedDate]) Then
SQLFields = SQLFields & "PurchasedDate, "
SQLValues = SQLValues & "#" & Me![tbPurchasedDate] & "#, "
End If
If Not IsNull(Me![tbInstalledDate]) Then
SQLFields = SQLFields & "InstalledDate, "
SQLValues = SQLValues & "#" & Me![tbInstalledDate] & "#, "
End If
If Not IsNull(Me![tbWarranteeUntil]) Then
SQLFields = SQLFields & "WarranteeUntil, "
SQLValues = SQLValues & "#" & Me![tbWarranteeUntil] & "#, "
End If
If Not IsNull(Me![tbTechFile]) Then
SQLFields = SQLFields & "TechnicalFile, "
SQLValues = SQLValues & "'" & Me![tbTechFile] & "', "
End If
If Not IsNull(Me![tbMFR]) Then
SQLFields = SQLFields & "Manufacturer, "
SQLValues = SQLValues & "'" & Me![tbMFR] & "', "
End If
'this field you hard coded to = 1
'If Not IsNull(Me![tblOutOfService]) Then
SQLFields = SQLFields & "OutOfService, "
SQLValues = SQLValues & 1 & ", "
'SQLValues = SQLValues & Me![tblOutOfService] & ", "
'End If
If Not IsNull(Me![tbOutUntil]) Then
SQLFields = SQLFields & "OutUntil, "
SQLValues = SQLValues & "#" & Me![tbOutUntil] & "#, "
End If
If Not IsNull(Me![tbTakenOut]) Then
SQLFields = SQLFields & "TakenOut, "
SQLValues = SQLValues & "#" & Me![tbTakenOut] & "#, "
End If
If Not IsNull(Me![tbLastReading]) Then
SQLFields = SQLFields & "LastReadingDate, "
SQLValues = SQLValues & "#" & Me![tbLastReading] & "#, "
End If
If Not IsNull(Me![tbCurrentHours]) Then
SQLFields = SQLFields & "CurrentHours, "
SQLValues = SQLValues & Me![tbCurrentHours] & ", "
End If
If Not IsNull(Me![tbCurrentMiles]) Then
SQLFields = SQLFields & "CurrentMiles, "
SQLValues = SQLValues & Me![tbCurrentMiles] & ", "
End If
If Not IsNull(Me![tbCurrentKM]) Then
SQLFields = SQLFields & "CurrentKM, "
SQLValues = SQLValues & Me![tbCurrentKM] & ", "
End If
SQLFields = Left(SQLFields, Len(SQLFields) - 2) & ")"
SQLValues = Left(SQLValues, Len(SQLValues) - 2) & ");"
MySQL = "INSERT INTO Equipment " & SQLFields & " " & SQLValues
Please post the code you are using now.
Steve
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)
The PK is EquipNo. When I left Numeric and date fields empty. The error
message is
INSERT INTO statement syntax error
If I don't left them empty, then It can insert record into the table.
Why?
I add NZ function into my SQL string, the error message is the same.
"SteveS" <sanfu@_KILLS-SPAM_techie.com> ????
Dou,
I just read Dirk's post; I didn't check to see what would happen if not
all data was entered. I left a couple of the boxes empty and of course
it bombed. In my defense, it was early in the morning.....
An alternative to adding NZ() to the SQL statement, since it is only a
text string, is to build the INSERT statement 'on the fly'.
Here it is:
Dim MySQL As String
Dim SQLFields As String
Dim SQLValues As String
SQLFields = "("
SQLValues = "VALUES("
If Not IsNull(Me![cbGroupCat]) Then
SQLFields = SQLFields & "GroupCategory, "
SQLValues = SQLValues & "'" & Me![cbGroupCat] & "', "
End If
If Not IsNull(Me![tbEquipNo]) Then
SQLFields = SQLFields & "EquipNo, "
SQLValues = SQLValues & "'" & Me![tbEquipNo] & "', "
End If
If Not IsNull(Me![tbEquipDesc]) Then
SQLFields = SQLFields & "EquipDesc, "
SQLValues = SQLValues & "'" & Me![tbEquipDesc] & "', "
End If
If Not IsNull(Me![tbAssetNo]) Then
SQLFields = SQLFields & "AssetNo, "
SQLValues = SQLValues & "'" & Me![tbAssetNo] & "', "
End If
If Not IsNull(Me![tbPartNo]) Then
SQLFields = SQLFields & "PartNo, "
SQLValues = SQLValues & "'" & Me![tbPartNo] & "', "
End If
If Not IsNull(Me![tbModelNo]) Then
SQLFields = SQLFields & "ModelNo, "
SQLValues = SQLValues & "'" & Me![tbModelNo] & "', "
End If
If Not IsNull(Me![tbSerialNo]) Then
SQLFields = SQLFields & "SerialNo, "
SQLValues = SQLValues & "'" & Me![tbSerialNo] & "', "
End If
If Not IsNull(Me![tbDetails]) Then
SQLFields = SQLFields & "EquipDetails, "
SQLValues = SQLValues & "'" & Me![tbDetails] & "', "
End If
If Not IsNull(Me![tbOriginCost]) Then
SQLFields = SQLFields & "OriginalCost, "
SQLValues = SQLValues & Me![tbOriginCost] & ", "
End If
If Not IsNull(Me![tbLocation]) Then
SQLFields = SQLFields & "Location, "
SQLValues = SQLValues & "'" & Me![tbLocation] & "', "
End If
If Not IsNull(Me![tbPurchasedDate]) Then
SQLFields = SQLFields & "PurchasedDate, "
SQLValues = SQLValues & "#" & Me![tbPurchasedDate] & "#, "
End If
If Not IsNull(Me![tbInstalledDate]) Then
SQLFields = SQLFields & "InstalledDate, "
SQLValues = SQLValues & "#" & Me![tbInstalledDate] & "#, "
End If
If Not IsNull(Me![tbWarranteeUntil]) Then
SQLFields = SQLFields & "WarranteeUntil, "
SQLValues = SQLValues & "#" & Me![tbWarranteeUntil] & "#, "
End If
If Not IsNull(Me![tbTechFile]) Then
SQLFields = SQLFields & "TechnicalFile, "
SQLValues = SQLValues & "'" & Me![tbTechFile] & "', "
End If
If Not IsNull(Me![tbMFR]) Then
SQLFields = SQLFields & "Manufacturer, "
SQLValues = SQLValues & "'" & Me![tbMFR] & "', "
End If
'this field you hard coded to = 1
'If Not IsNull(Me![tblOutOfService]) Then
SQLFields = SQLFields & "OutOfService, "
SQLValues = SQLValues & 1 & ", "
'SQLValues = SQLValues & Me![tblOutOfService] & ", "
'End If
If Not IsNull(Me![tbOutUntil]) Then
SQLFields = SQLFields & "OutUntil, "
SQLValues = SQLValues & "#" & Me![tbOutUntil] & "#, "
End If
If Not IsNull(Me![tbTakenOut]) Then
SQLFields = SQLFields & "TakenOut, "
SQLValues = SQLValues & "#" & Me![tbTakenOut] & "#, "
End If
If Not IsNull(Me![tbLastReading]) Then
SQLFields = SQLFields & "LastReadingDate, "
SQLValues = SQLValues & "#" & Me![tbLastReading] & "#, "
End If
If Not IsNull(Me![tbCurrentHours]) Then
SQLFields = SQLFields & "CurrentHours, "
SQLValues = SQLValues & Me![tbCurrentHours] & ", "
End If
If Not IsNull(Me![tbCurrentMiles]) Then
SQLFields = SQLFields & "CurrentMiles, "
SQLValues = SQLValues & Me![tbCurrentMiles] & ", "
End If
If Not IsNull(Me![tbCurrentKM]) Then
SQLFields = SQLFields & "CurrentKM, "
SQLValues = SQLValues & Me![tbCurrentKM] & ", "
End If
SQLFields = Left(SQLFields, Len(SQLFields) - 2) & ")"
SQLValues = Left(SQLValues, Len(SQLValues) - 2) & ");"
MySQL = "INSERT INTO Equipment " & SQLFields & " " & SQLValues
'MsgBox MySQL
Steve