web it support and consulting - run multiple queries in ms access vba and check result
DESCRIPTION
Just imagine that you inherit a MS Access database with ODBC connections to hundreds of dBase files (DBF) and you need to run 1 TIME a lot of action queries to insert that data into a new MS SQL Server database. Not an easy task, lots of efforts and really time consuming. That’s why I want to share the next solution with you.TRANSCRIPT
![Page 1: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/1.jpg)
Web-IT Support and Consulting
Web-IT Support and Consulting
A human vision on the digital world
![Page 2: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/2.jpg)
Web-IT Support and Consulting
Still using dBase?
Just imagine that you inherit a MS Access database with ODBC connections to hundreds of dBase files (DBF) and you need to run 1 TIME a lot of action queries to insert that data into a new
MS SQL Server database.
Not an easy task, lots of efforts and really time consuming.That’s why I want to share the next solution with you
(download the slide share file to use the copy-paste function).
Enjoy reading and for any additional question,just give a ring to my team (info at the last page).
Cheeriooo,Webby (that’s me)
![Page 3: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/3.jpg)
Web-IT Support and Consulting
Why this solution
My boss and his (my) team always have creative
solutions and like to share them with you.
Fastest way to run multiple action queries from VBA code
Progress screen to keep track of the inserts
Check if all data has been migrated (counts)
![Page 4: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/4.jpg)
Web-IT Support and Consulting
Code to clear all tables first
Yep, this looks easy to me.
Create a stored procedure on MS SQL Server
Execute this query from your MS Access database
CREATE PROCEDURE [dbo].[SPClearAllTables]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Remove data from all tables
EXEC sp_MSforeachtable 'TRUNCATE TABLE ?'
END
EXEC dbo.SPClearAllTables
![Page 5: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/5.jpg)
Web-IT Support and Consulting
Code to clear all tables first
It’s even more user friendly to do it from a form so you can givefeedback to the users
If you like to run the query from a button on a form this is the code
Private Sub BttnClearData_Click()
‘ Ask user for confirmation
If MsgBox(“Please confirm that you want to remove the content from all MS SQL Server tables.",
vbCritical + vbYesNo, “User action required") = vbYes Then
‘ Remove warnings given by MS Access
DoCmd.SetWarnings False
‘ Run the query that executes the stored procedure (see previous page)
DoCmd.OpenQuery "RunClearTables", acViewNormal
‘ Set warnings back on
DoCmd.SetWarnings True
‘ Confirm end of process
MsgBox "All records have been removed from the MS SQL Server tables", vbInformation
Else
‘ Message when process cancelled by the user
MsgBox “Process cancelled by the user.", vbInformation
End If
End Sub
![Page 6: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/6.jpg)
Web-IT Support and Consulting
Code to verify the inserts
You can find so manyinformation in those system tables. I really should learn
more about that.
Create a stored procedure on MS SQL Server
Execute it later in your VBA code
CREATE PROCEDURE [dbo].[SpUpdateImportResults]
AS
BEGIN
-- Update statement
UPDATE dbo.TblImportResult
SET ImpDestinationCount = TempCount.TableRowCount
FROM dbo.TblImportResult
INNER JOIN
-- Select all tables from the system objects (sys.objects)
-- Select the number of rows from the system partitions (sys.partitions)
(SELECT 'dbo.' + sys.objects.name As DestTable, sys.partitions.rows As TableRowCount
FROM sys.objects INNER JOIN sys.partitions
ON sys.objects.object_id = sys.partitions.object_id
WHERE sys.objects.type = 'U'
) TempCount
ON dbo.TblImportResult.ImpDestination = TempCount.DestTable
END
![Page 7: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/7.jpg)
Web-IT Support and Consulting
Insert the records
The OnClick event can be found in the properties of the button.
Luckily I’m here to mention all those detailsand fine tuning.
It’s a dirty job, but someone’s got to do it !
Create this VBA code and activate it from an ‘OnClick’ event of a button on your form (part 1)
Private Sub BttnImportData_Click()
‘ Define all the variables
Dim DbDef As Database
Dim RstSource As Recordset
Dim RstDestination As Recordset
Dim QryDef As QueryDef
Dim CntUpdate As Integer
Dim CntQry As Integer
Dim SourceCount As Integer
Dim SqlInsert As String
Dim Source As String
Dim Destination As String
![Page 8: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/8.jpg)
Web-IT Support and Consulting
Insert the records
Copy-paste this code behind the previous
code.
Create this VBA code and activate it from an ‘OnClick’ event of a button on your form (part 2)
‘ Initialize the variables
Set DbDef = CurrentDb
CntUpdate = 1
CntQry = 0
SourceCount = 0
SqlInsert = ""
Source = ""
Destination = ""
![Page 9: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/9.jpg)
Web-IT Support and Consulting
Insert the records
Copy-paste this code behind the previous
code.
Create this VBA code and activate it from an ‘OnClick’ event of a button on your form (part 3)
‘ Count the number of queries starting with ‘Qry’ to give this feedback on the
screen while the user is waiting
For Each QryDef In CurrentDb.QueryDefs
If Left(QryDef.Name, 3) = "Qry" Then
CntQry = CntQry + 1
End If
Next QryDef
![Page 10: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/10.jpg)
Web-IT Support and Consulting
Insert the records
Copy-paste this code behind the previous
code.
Create this VBA code and activate it from an ‘OnClick’ event of a button on your form (part 4)
DoCmd.SetWarnings False
‘ Loop through all queries
For Each QryDef In CurrentDb.QueryDefs
If Left(QryDef.Name, 3) = "Qry" Then
‘ Inform user on screen what is the status (x/y)
Me.TxtCounter.Value = CntUpdate & " / " & CntQry
‘ Set source and destination name and return it on the screen
Source = Mid(QryDef.Name, 4, Len(QryDef.Name))
Destination = "dbo." & Mid(QryDef.Name, 4, Len(QryDef.Name))
Me.TxtUpdatingTable.Value = Destination
Me.Repaint
‘ This line will let the code continue even if there was an error
‘ No worries to do so since we do a result check at the end
On Error Resume Next
‘ Run action query
DoCmd.OpenQuery QryDef.Name, acViewNormal
![Page 11: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/11.jpg)
Web-IT Support and Consulting
Insert the records
Copy-paste this code behind the previous code.
Create this VBA code and activate it from an ‘OnClick’ event of a button on your form (part 5)
‘ Open a recordset to count the amount of records that need to be uploaded
Set RstSource = DbDef.OpenRecordset(Source)
‘ Populate the recordset
RstSource.MoveFirst
‘ Count the rows
‘ I do it this way because a .MoveLast is not always returning correct values
‘ if you have a large amount of data
SourceCount = 0
Do While Not RstSource.EOF
SourceCount = SourceCount +1
RstSource.MoveNext
Loop
RstSource.Close
‘ Insert the information in your feedback table on MS SQL Server
SqlInsert = "INSERT INTO dbo_TblImportResult (ImpSource, ImpDestination, ImpSourceCount)" _
& " SELECT '" & Source & "' AS ImpSource, '" & Destination & "' AS
ImpDestination, " & SourceCount & " AS ImpSourceCount;"
DoCmd.RunSQL (SqlInsert)
![Page 12: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/12.jpg)
Web-IT Support and Consulting
Insert the records
Copy-paste this code behind the previous
code.
Create this VBA code and activate it from an ‘OnClick’ event of a button on your form (part 6)
‘ Bring back the error resuming to its original state
On Error GoTo 0
CntUpdate = CntUpdate + 1
End If
Next QryDef
‘ Run stored procedure to update the feedback table with the number of inserted rows
‘ in the MS SQL Server tables
DoCmd.OpenQuery "RunUpdateImportResults", acViewNormal
DoCmd.SetWarnings True
‘ Inform the user the process is finished
MsgBox "Import procedure is finished. Please check the result table on MS SQL Server.",
vbInformation
End Sub
![Page 13: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/13.jpg)
Web-IT Support and Consulting
One last thing before I let you work…
I am so proud about these 2 kids.They saw immediately that
Web-IT needed somebody like me (smart, good looking, brains, ...)
One day the 2 little daughters of the owners were playing with the PC of the old man
They saw a presentation of Web-IT Support and Consulting and they asked what W,E,B,-,I,T was
My parents explained that it was the name of the company and they started to make funny noises
Webbit, webbiiiit, webbbi, webbeee and they said it sounded like the sound of a frog
Result?My name: WebbyMy body: a frog
Why is my name is Webby?
![Page 14: Web it support and consulting - run multiple queries in ms access vba and check result](https://reader033.vdocuments.mx/reader033/viewer/2022042816/558e19651a28abcb3f8b45bc/html5/thumbnails/14.jpg)
Web-IT Support and Consulting
It might seem a bit weird that I take over the entire (and last) screen.
But it’s my job to end this case study.I would be glad to give an answer to all your questions
and remarks.Just call or e-mail me.
Oh yeah, ask for Webby. They’ll know where to find me.Thanks for your attention and hope to hear you soon.
Web-IT Support and Consulting bvbaSteenhuffelstraat 8, 1861-Wolvertem
E-mail: [email protected]
Phone: +32 (0)2 309 67 76Mobile: +32 (0)475 59 98 20
GPS: latitude: 50.97395, longitude: 4.30504