Hello there,
I'm having an issue with a back-end database that I am working on, the connection starts absolutely fine and with just the query on its own I am able to connect and execute the INSERT INTO SQL query without a problem.
The problem seems to be when I'm trying to run another query after selecting the data from the database.
The variables I'm attempting to input are all global as this is the front end. However it is the following line giving me the problem:
I am not sure why it is doing this as I mentioned before it connects just fine normally and I am able to create new records but not when I'm attempting to do this.
The error message is
Thanks,
JC.
I'm having an issue with a back-end database that I am working on, the connection starts absolutely fine and with just the query on its own I am able to connect and execute the INSERT INTO SQL query without a problem.
The problem seems to be when I'm trying to run another query after selecting the data from the database.
Code:
Sub dbUpdate()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strsql As String
'Setup DB connections
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:=Cnct
Set rst = New ADODB.Recordset
'Declare variables
Dim EDate As Date 'Current date
Dim StartTime As Date
Dim Time As Date
'Error handling:
' On Error GoTo closeDB
'Set variables
EDate = Format(Now(), "DD/MM/YYYY")
UserName = Environ("Username")
StartTime = Format(Now(), "hh:mm")
Time = StartTime
'Set string to send to DB
strsql = "SELECT * FROM Clicker WHERE UserName = '" & UserName & "' AND EDate = " & EDate
'Open DB using string
rst.Open strsql, cnn, adOpenStatic
'If end of file then create record
If rst.EOF Then
With cnn
strsql = "INSERT INTO Clicker ( EDate, UserName, WiB, DD, Refund, StartTime, Time )" & _
"VALUES ('" & EDate & "','" & UserName & "','" & WiB & "','" & DD & "','" & Refund & "','" & StartTime & _
"','" & Time & "')"
.Execute strsql
End With
Else
With cnn
strsql = "UPDATE Clicker SET WiB = " & WiB & ", DD = " & DD & ", Refund = " & Refund & _
"WHERE EDate = '" & EDate & "' AND Username = '" & UserName & ";"
.Execute strsql
End With
End If
'closeDB:
'Close DB
cnn.Close
Set rst = Nothing
Set cnn = Nothing
If Err.Description <> "" Then
MsgBox "Problem with Database connection: " & vbCrLf & Err.Description
End If
End Sub
The variables I'm attempting to input are all global as this is the front end. However it is the following line giving me the problem:
Code:
With cnn
strsql = "INSERT INTO Clicker ( EDate, UserName, WiB, DD, Refund, StartTime, Time )" & _
"VALUES ('" & EDate & "','" & UserName & "','" & WiB & "','" & DD & "','" & Refund & "','" & StartTime & _
"','" & Time & "')"
.Execute strsql
End With
I am not sure why it is doing this as I mentioned before it connects just fine normally and I am able to create new records but not when I'm attempting to do this.
The error message is
Syntax error in INSERT INTO statement.
Thanks,
JC.