Barbacuca
New member
- Local time
- Today, 09:23
- Joined
- May 13, 2009
- Messages
- 9
Hello,
I'm trying to use VBA to implement a routine to automate queries and updates which must happen in sequence. Given that I am but a beginner in VBA coding, I'm struggling to make this work.
Below you'll find the code where I use DoCmd.RunSQL several times. I'd rather declare the SQL statements as Strings and then run them, but I don't know how to do it. Further this code below only works up to a point and then throws and error about misuse of the SELECT statement...
Thanks for your help!
Barbacuca
#### CODE ####
' *** IMPORT NEW CMTO ***
'
' This routine perfomrs 5 actions:
' 1) Deletes current content in CMTO table (qry_deleteCMTOrecords)
' 2) Imports table CMTO_ST.xls (DoCmd.TransferSpreadsheet)
' 3) Deletes content in lutbl_Identifier (qry_deleteIdentifier)
' 4) Finds all unique new identifiers in identifier-field in CMTO (qry_findAllPossibleIdentifier)
' 5) Appends new identifiers to lutbl_Identifier (qry_appendNewIdentifier)
Private Sub ImportNewCMTO_Click()
On Error GoTo ImportNewCMTO_Click_Err
' Deletes old CMTO contents
DoCmd.RunSQL "DELETE CMTO.*" _
& "FROM CMTO;", -1
' Imports CMTO
DoCmd.TransferSpreadsheet acImport, 8, "CMTO", "" _
& "W:\Project\Controls\" _
& "Estimate\System\Sources\CMTO_ST.xls", True, ""
' Deletes old identifier contents
DoCmd.RunSQL "DELETE lutbl_Identifier.*" _
& "FROM lutbl_Identifier;"
' Finds new identifiers
DoCmd.RunSQL "SELECT First(CMTO.identifier) AS [identifier Field], Count(CMTO.identifier) AS NumberOfDups" _
& "FROM CMTO" _
& "GROUP BY CMTO.identifier" _
& "HAVING (((Count(CMTO.identifier))>1));"
' Appends new identifiers to Identifier table
DoCmd.RunSQL "INSERT INTO lutbl_Identifier" _
& "SELECT qry_findAllPossibleIdentifiers.*" _
& "FROM qry_findAllPossibleIdentifiers;"
ImportNewCMTO_Click_Exit:
Exit Sub
ImportNewCMTO_Click_Err:
MsgBox Error$
Resume ImportNewCMTO_Click_Exit
End Sub
I'm trying to use VBA to implement a routine to automate queries and updates which must happen in sequence. Given that I am but a beginner in VBA coding, I'm struggling to make this work.
Below you'll find the code where I use DoCmd.RunSQL several times. I'd rather declare the SQL statements as Strings and then run them, but I don't know how to do it. Further this code below only works up to a point and then throws and error about misuse of the SELECT statement...
Thanks for your help!
Barbacuca
#### CODE ####
' *** IMPORT NEW CMTO ***
'
' This routine perfomrs 5 actions:
' 1) Deletes current content in CMTO table (qry_deleteCMTOrecords)
' 2) Imports table CMTO_ST.xls (DoCmd.TransferSpreadsheet)
' 3) Deletes content in lutbl_Identifier (qry_deleteIdentifier)
' 4) Finds all unique new identifiers in identifier-field in CMTO (qry_findAllPossibleIdentifier)
' 5) Appends new identifiers to lutbl_Identifier (qry_appendNewIdentifier)
Private Sub ImportNewCMTO_Click()
On Error GoTo ImportNewCMTO_Click_Err
' Deletes old CMTO contents
DoCmd.RunSQL "DELETE CMTO.*" _
& "FROM CMTO;", -1
' Imports CMTO
DoCmd.TransferSpreadsheet acImport, 8, "CMTO", "" _
& "W:\Project\Controls\" _
& "Estimate\System\Sources\CMTO_ST.xls", True, ""
' Deletes old identifier contents
DoCmd.RunSQL "DELETE lutbl_Identifier.*" _
& "FROM lutbl_Identifier;"
' Finds new identifiers
DoCmd.RunSQL "SELECT First(CMTO.identifier) AS [identifier Field], Count(CMTO.identifier) AS NumberOfDups" _
& "FROM CMTO" _
& "GROUP BY CMTO.identifier" _
& "HAVING (((Count(CMTO.identifier))>1));"
' Appends new identifiers to Identifier table
DoCmd.RunSQL "INSERT INTO lutbl_Identifier" _
& "SELECT qry_findAllPossibleIdentifiers.*" _
& "FROM qry_findAllPossibleIdentifiers;"
ImportNewCMTO_Click_Exit:
Exit Sub
ImportNewCMTO_Click_Err:
MsgBox Error$
Resume ImportNewCMTO_Click_Exit
End Sub