mparker1061
New member
- Joined
- Apr 13, 2011
- Messages
- 3
- Programming Experience
- 5-10
When I try to insert a record into an Access 2010 database, I get the error "Syntax error in INSERT INTO statement". If I copy the SQL and run it using a query in Access, the record inserts fine. The SQL statement is:
INSERT INTO tbDevices(DeviceName, DeviceType, ClosedLS, OpenLS, Output, Route, Dest0) VALUES('A1004', 'S', 'B3/9', 'B3/10', 'B3/11', 'N7:3', 'A1005')
The following code:
PrivateSub cmdNew_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles cmdNew.Click
Dim cnCon AsOleDbCommand
Dim strSql AsString
Dim strInsert AsString
Dim strValues AsString
If Trim$(txtDeviceName.Text) = ""Then
MsgBox("Please enter a valid device name.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf cbType.SelectedIndex < 0 Then
MsgBox("Please select a valid device type.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf Trim$(txtOutput.Text) = ""Then
MsgBox("Please enter a valid output.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf Trim$(txtRoute.Text) = ""Then
MsgBox("Please enter a valid route.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf cbType.Text <> "D"And Trim$(txtDest0.Text) = ""Then
MsgBox("Please enter a valid destination 0.", MsgBoxStyle.Exclamation)
Exit Sub
EndIf
Try
strInsert = "INSERT INTO tbDevices(DeviceName, DeviceType, "
strValues = "VALUES('" & UCase$(txtDeviceName.Text) & "', '" & cbType.Text & "', '"
If Trim$(txtCLS.Text) <> ""Then
strInsert += "ClosedLS, "
strValues += UCase$(txtCLS.Text) & "', '"
EndIf
If Trim$(txtOLS.Text) <> ""Then
strInsert += "OpenLS, "
strValues += UCase$(txtOLS.Text) & "', '"
EndIf
strInsert += "Output, Route"
strValues += UCase$(txtOutput.Text) & "', '" & UCase$(txtRoute.Text) & "'"
If Trim$(txtDest0.Text) <> ""Then
strInsert += ", Dest0"
strValues += ", '" & UCase$(txtDest0.Text) & "'"
EndIf
If Trim$(txtDest1.Text) <> ""Then
strInsert += ", Dest1"
strValues += ", '" & UCase$(txtDest1.Text) & "'"
EndIf
strInsert += ") "
strValues += ")"
strSql = strInsert & strValues
gsCon.Open()
cnCon = NewOleDbCommand(strSql, gsCon)
cnCon.ExecuteNonQuery()
gsCon.Close()
Call ClearForm()
Catch ex AsException
gsCon.Close()
MsgBox(Err.Description)
EndTry
EndSub
gsCon is defined:
Private gsCon AsOleDbConnection
gsCon = NewOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Environment.CurrentDirectory & "\Devices.mdb;Persist Security Info=False")
Any suggestions?
INSERT INTO tbDevices(DeviceName, DeviceType, ClosedLS, OpenLS, Output, Route, Dest0) VALUES('A1004', 'S', 'B3/9', 'B3/10', 'B3/11', 'N7:3', 'A1005')
The following code:
PrivateSub cmdNew_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles cmdNew.Click
Dim cnCon AsOleDbCommand
Dim strSql AsString
Dim strInsert AsString
Dim strValues AsString
If Trim$(txtDeviceName.Text) = ""Then
MsgBox("Please enter a valid device name.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf cbType.SelectedIndex < 0 Then
MsgBox("Please select a valid device type.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf Trim$(txtOutput.Text) = ""Then
MsgBox("Please enter a valid output.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf Trim$(txtRoute.Text) = ""Then
MsgBox("Please enter a valid route.", MsgBoxStyle.Exclamation)
Exit Sub
ElseIf cbType.Text <> "D"And Trim$(txtDest0.Text) = ""Then
MsgBox("Please enter a valid destination 0.", MsgBoxStyle.Exclamation)
Exit Sub
EndIf
Try
strInsert = "INSERT INTO tbDevices(DeviceName, DeviceType, "
strValues = "VALUES('" & UCase$(txtDeviceName.Text) & "', '" & cbType.Text & "', '"
If Trim$(txtCLS.Text) <> ""Then
strInsert += "ClosedLS, "
strValues += UCase$(txtCLS.Text) & "', '"
EndIf
If Trim$(txtOLS.Text) <> ""Then
strInsert += "OpenLS, "
strValues += UCase$(txtOLS.Text) & "', '"
EndIf
strInsert += "Output, Route"
strValues += UCase$(txtOutput.Text) & "', '" & UCase$(txtRoute.Text) & "'"
If Trim$(txtDest0.Text) <> ""Then
strInsert += ", Dest0"
strValues += ", '" & UCase$(txtDest0.Text) & "'"
EndIf
If Trim$(txtDest1.Text) <> ""Then
strInsert += ", Dest1"
strValues += ", '" & UCase$(txtDest1.Text) & "'"
EndIf
strInsert += ") "
strValues += ")"
strSql = strInsert & strValues
gsCon.Open()
cnCon = NewOleDbCommand(strSql, gsCon)
cnCon.ExecuteNonQuery()
gsCon.Close()
Call ClearForm()
Catch ex AsException
gsCon.Close()
MsgBox(Err.Description)
EndTry
EndSub
gsCon is defined:
Private gsCon AsOleDbConnection
gsCon = NewOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Environment.CurrentDirectory & "\Devices.mdb;Persist Security Info=False")
Any suggestions?