Consulting

Results 1 to 4 of 4

Thread: Run Access create table query from Excel with msgbox displaying records created

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    18
    Location

    Run Access create table query from Excel with msgbox displaying records created

    Hi,

    I have an Excel spreadsheet where I can click on a button. When I click the button a table is created in an Access database. How can I in the following vba code where the msgbox is shown count the records that are created in Access?

    Sub GenopfriskMDM()
     
    Dim AppAcc, AppAccDatabase
    Set AppAcc = CreateObject("Access.Application")
    
    'Set Database Path & Name
    AppAccDatabase = "P:\KoncernOkonomi\indkobsafdelingen\ANALYSEAFDELINGEN\ACCESS\MDM Dublet tjek\test_MDM_data_c.accdb"
    With AppAcc
    'Open Database
    .OpenCurrentDatabase AppAccDatabase
    'Run Query
    .DoCmd.OpenQuery ("q_tblUdtræk2")
    End With
      
      
    MsgBox "VBA \ MDM data genopfrisket"
    End Sub

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You could use ADO:
    Sub ExecuteAccessActionQuery()
    ' Sample demonstrating how to execute an action query in an Access db
       Dim strQuery                    As String
       Dim strPathToDB                 As String
       Dim lngNumRecs                    As Long
       
       Const adCmdStoredProc As Long = 4
       ' Change path as necessary
       strPathToDB = "P:\KoncernOkonomi\indkobsafdelingen\ANALYSEAFDELINGEN\ACCESS\MDM Dublet tjek\test_MDM_data_c.accdb"
       
       ' change query name
       strQuery = "q_tblUdtræk2"
    
       With CreateObject("ADODB.Connection")
          .Provider = "Microsoft.ACE.OLEDB.12.0"
          .ConnectionString = "Data Source=" & strPathToDB & ";"
          .Open
          .Execute strQuery, lngNumRecs, adCmdStoredProc
          .Close
       End With
       MsgBox lngNumRecs & " records added"
    
    End Sub
    for example.
    Be as you wish to seem

  3. #3
    VBAX Regular
    Joined
    Oct 2010
    Posts
    18
    Location
    Thanks a lot.

    I get this error: Run-time error '-2147217900 (80040e14)': Undefined function "removechars" in expression at the stage:

    .Execute strQuery, lngNumRecs, adCmdStoredProc
    It's because I have two functions in the Access database that strip the supplier item number for special characters. The two functions are: Trim_varenr: removeChars([tblMDM.Leverandørdelnummer]) and Trim_nul: StripZeros([Trim_varenr]).

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You cannot use ADO then. Perhaps:
    [vba]
    Sub GenopfriskMDM()

    Dim AppAcc, AppAccDatabase, db
    Set AppAcc = CreateObject("Access.Application")

    'Set Database Path & Name
    AppAccDatabase = "P:\KoncernOkonomi\indkobsafdelingen\ANALYSEAFDELINGEN\ACCESS\MDM Dublet tjek\test_MDM_data_c.accdb"
    With AppAcc
    'Open Database
    set db = .OpenCurrentDatabase(AppAccDatabase)
    'Run Query
    db.execute "q_tblUdtræk2"
    msgbox db.recordsaffected & " records added"
    End With


    MsgBox "VBA \ MDM data genopfrisket"
    End Sub[/vba]
    Be as you wish to seem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •