
Connect to the database using what application? If you're asking how to connect an Access database front end to an Access database back end file, then you can check out the Connect property of the TableDef object.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I want to connect to my local access database in VBA. How do I find or make the details in this string that I need to do this connection?
Connect to the database using what application? If you're asking how to connect an Access database front end to an Access database back end file, then you can check out the Connect property of the TableDef object.
And if you don't already have that link, you can create it with the
External Data => New Data Source => From Database => Access
option on the ribbon.
The following is a code snippet which creates a temporary external database and creates two tables in it, then creates links to those tables in the current database. You should be able to extract examples for necessary code for what you want to do from this:
' create temporary database in same folder as current database
' or return reference to database if already exists
strTempDb = Left$(dbs.Name, Len(dbs.Name) - Len(Dir(dbs.Name))) & "BoMTemp.accdb"
On Error Resume Next
Set dbsTemp = CreateDatabase(strTempDb, dbLangGeneral)
If Err <> 0 Then
Set dbsTemp = OpenDatabase(strTempDb)
End If
On Error GoTo Err_Handler
' create BoM table in temporary database if doesn't exist
strSQL = "CREATE TABLE BoM (MajorPartNum LONG NOT NULL, " & _
"MinorPartNum LONG NOT NULL, Quantity LONG NOT NULL, " & _
"CONSTRAINT MajorMinor PRIMARY KEY (MajorPartNum, MinorPartNum) )"
On Error Resume Next
dbsTemp.Execute (strSQL)
On Error GoTo Err_Handler
' create BoM_Temp table in temporary database.
' note that this one has no constraint as it will be
' necessary to add duplicates of major/minor part number
' values in separate rows
strSQL = "CREATE TABLE BoM_Temp (MajorPartNum LONG NOT NULL, " & _
"MinorPartNum LONG NOT NULL, Quantity LONG NOT NULL)"
dbsTemp.Execute (strSQL)
' check if link to BoM_Temp table exists and if not create link
On Error Resume Next
Set tdf = dbs.TableDefs("BoM_Temp")
If Err <> 0 Then
DoCmd.TransferDatabase acLink, "Microsoft Access", strTempDb, acTable, "BoM_Temp", "BoM_Temp"
Else
varFld = tdf.Fields(0)
If Err <> 0 Then
' refresh link if current link invalid
tdf.Connect = ";DATABASE=" & strTempDb
tdf.RefreshLink
End If
End If
Err.Clear
' check if link to BoM table exists and if not create link
Set tdf = dbs.TableDefs("BoM")
If Err <> 0 Then
DoCmd.TransferDatabase acLink, "Microsoft Access", strTempDb, acTable, "BoM", "BoM"
Else
varFld = tdf.Fields(0)
If Err <> 0 Then
' refresh link if current link invalid
tdf.Connect = ";DATABASE=" & strTempDb
tdf.RefreshLink
End If
End If
Err.Clear
You'll find the code in Bom.accdb in Bom..zip in my Dropbox public databases folder at:
For means of browsing to and opening a file from within Access take a look at BrowseDemo.zip in the same Dropbox folder to which I gave you a link in my last reply. Oops. That was meant as a reply to another thread.