Excel - Access SQL query problem

JOCoyle

New Member
Joined
Sep 30, 2016
Messages
11
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.

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.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,215,368
Messages
6,124,520
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top