Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:58:17+00:00 2026-05-24T05:58:17+00:00

I have a subroutine in excel that I would like to write data to

  • 0

I have a subroutine in excel that I would like to write data to an Access table. I am trying to update a row in the table if it already exists, add it if it does not. Going off of the suggestion given on this MSDN blog by Jeremiah Clark, I have my query that I will execute from my Excel’s VBA subroutine:

UPDATE tblName 
SET  [Column1] = 'text', ...(other values)... [ColumnN] = 1234 

WHERE ([Column1] = 'text' AND [Column2] = 'text2')

If @@ROWCOUNT = 0

INSERT INTO  tblName

VALUES   ( [Column1] = 'text', ...(other values)... [ColumnN] = 1234 )

The error it gives me is:

Syntax error (missing operator) in query expression '([Column1] = 'text' AND [Column2] = 'text2')

If @@ROWCOUNT = 0

INSERT INTO  tblName

VALUES   ( [Column1] = 'text', ...(other values)...'.

I’m pretty new to SQL, but have tried various ways of bracketing (parentheses-ing) the IF line in case the evaluation order was not what I expected, but that was to no avail. Is the first part of the query not being evaluated and thus @@ROWCOUNT cannot be executed properly?

Edit1: Using Access 2003 if that matters.

Solution:
Based on bluefeet’s suggestion (see his entire response):

objDB.Execute sqlStrSelect
recordset.Source = sqlStrSelect
recordset.Open , , adOpenDynamic, adLockOptimistic

If recordset.Fields(0) = 0 Then
  objDB.Execute sqlStrInsert
Else
  objDB.Execute sqlStrUpdate
End If

This relies on a modified SELECT query to get Access to return the Count of the records:

sqlStrSelect = "SELECT Count(id) FROM table1 WHERE id = 3"

HansUp correctly surmised that I was using an ADO connection, so executing the code had to be done differently from what bluefeet originally suggested.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:58:18+00:00Added an answer on May 24, 2026 at 5:58 am

    The @@ROWCOUNT is used for SQL Server not Access but since you are using VBA you could do something similar to this. Basically create your SQL statements as strings putting in your values that you are checking for. Then run query against the table first to see if the record exists, if it does then do the UPDATE if not then do the INSERT. I quickly tested this in MS Access 2003 and it works.

    Public Sub test()
        Dim sqlStrUpdate As String
        Dim sqlStrSelect As String
        Dim sqlStrInsert As String
        Dim recordSet As recordSet
    
        sqlStrUpdate = "UPDATE table1 SET Field1 = " & 5 & " WHERE id = 3"
    
        sqlStrSelect = "SELECT id FROM table1 WHERE id = 3"
    
        sqlStrInsert = "INSERT INTO table1 (id, Field1, Field2, Field3) VALUES (3, 5, 0, 0)"
    
        Set recordSet = CurrentDb.OpenRecordset(sqlStrSelect)
    
        If recordSet.RecordCount > 0 Then
            DoCmd.RunSQL (sqlStrUpdate)
        ElseIf recordSet.RecordCount = 0 Then
            DoCmd.RunSQL (sqlStrInsert)
        End If
    
    End Sub
    

    EDIT: As HansUp pointed out you are querying from Excel, your code could be similar to this:

    Public Sub test_new()
    Dim cDir_Database         As String
    Dim DB_Conn               As New ADODB.Connection         'Access Connection
    Dim DB_RSet               As New ADODB.recordSet          'Access Record Set
    
    Dim sqlStrUpdate As String
    Dim sqlStrInsert As String
    
    cDir_Database = ".\.\.AccessBD.mdb "
    
    sqlStrUpdate = "UPDATE table1 SET Field1 = " & 10 & " WHERE id = 4"
    
    sqlStrInsert = "INSERT INTO table1 (id, Field1, Field2, Field3) VALUES (4, 5, 0, 0)"
    
            DB_Conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
                                "DBQ=" & cDir_Database & ";"
            DB_Conn.Open
            DB_Conn.BeginTrans
    
            DB_RSet.Open "SELECT id FROM table1 WHERE id = 4", DB_Conn, adOpenStatic, adLockReadOnly
    
            If DB_RSet.RecordCount > 0 Then
                DB_Conn.Execute (sqlStrUpdate)
            ElseIf DB_RSet.RecordCount = 0 Then
                DB_Conn.Execute (sqlStrInsert)
            End If
    
            DB_RSet.Close
            DB_Conn.CommitTrans
            DB_Conn.Close
    
    End Sub
    

    This has been tested from Excel 2003 to Access 2003 and worked. You need to have the references added to your Excel file for the Microsoft ActiveX Data Objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a base class object, I have an overridable subroutine that I would like
I would like to have a subroutine as a member of a hash which
I have a subroutine in Perl that should be indented like this: sub GetFiles
I have VB.Net subroutine that I am trying to convert to Powershell. I am
I have a subroutine that takes a filehandle as an argument. How do I
I often have a subroutine in Perl that fills an array with some information.
hi folks i have a subroutine called CheckDate() in the code behind. How would
Let's say I have a subroutine/method that a user can call to test some
I have code that calls an ENTRY in a SUBROUTINE before the SUBROUTINE .
I have a subroutine that changes its operation slightly to include either one list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.