I have access file that was currently using linked table, but I didn't want it to always check connection to linked table at start because some users don't have access to the server, but they need to use. That's why i wanted to create vba script that will connect to sql server, and fetch data to put into local table. And also this file (as application or still as a file will be on SharePoint for ease of access for users who don't have access to the DB.
I already have code that works but it iterates import row by row and since my sql query is returning >30000 rows it takes a lot of time.
I wanted to take different approach and this is how far i could go but it still gives me an error.
Any help would be appreciated
I already have code that works but it iterates import row by row and since my sql query is returning >30000 rows it takes a lot of time.
I wanted to take different approach and this is how far i could go but it still gives me an error.
Any help would be appreciated
VBA Code:
Option Compare Database
Sub ImportDataFromSQLServer()
On Error GoTo ErrorHandler
' Set connection parameters
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
' Set query timeout to 40 seconds (adjust as needed)
conn.CommandTimeout = 40
' Connection string for SQL Server (replace placeholders with your actual values)
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=server05p;Initial Catalog=REG_MSCRM;Integrated Security=SSPI;"
' Open the connection
conn.Open
' Create a local table in Access (replace with your actual table name)
Dim localTableName As String
localTableName = "Customer_data"
' Set up a recordset to store the retrieved data
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
' Delete existing data from the local table
DoCmd.SetWarnings False ' Disable warnings
DoCmd.OpenQuery "1_RemoveLocalData", acViewNormal, acReadOnly
DoCmd.SetWarnings True ' Enable warnings
' Check if TempImportTable exists, and if so, delete it
If TableExists("TempImportTable") Then
DoCmd.DeleteObject acTable, "TempImportTable"
End If
' Create a temporary table with the same structure as the result set
CurrentDb.Execute "SELECT TOP 0 * INTO [TempImportTable] FROM [Rep_customer] C", dbFailOnError
' Define your SQL query directly in VBA
Dim strSQL As String
strSQL = "SELECT * FROM Rep_customer AS C"
' Open the recordset with the SQL query
rs.Open strSQL, conn
' Create an ADO recordset for the temporary table
Dim tempTableRs As Object
Set tempTableRs = CreateObject("ADODB.Recordset")
tempTableRs.Open "TempImportTable", conn, adOpenKeyset, adLockOptimistic
' Copy the data from the original recordset to the temporary table
tempTableRs.CopyFromRecordset rs
' Insert data from the temporary table into the local table
DoCmd.RunSQL "INSERT INTO Customer_data SELECT * FROM TempImportTable"
' Close the recordset
rs.Close
tempTableRs.Close
' Optional: Compact and repair the database to free up space
DBEngine.CompactDatabase CurrentDb.Name, CurrentDb.Name
' Display a message (optional)
MsgBox "Data imported successfully!", vbInformation
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description, vbExclamation
End Sub
Function TableExists(tableName As String) As Boolean
Dim tdf As DAO.TableDef
For Each tdf In CurrentDb.TableDefs
If tdf.Name = tableName Then
TableExists = True
Exit Function
End If
Next tdf
TableExists = False
End Function