hi Bruce,
I am linking to SQL tables. On one of them, the "Select Unique Record
Identifier" dialog box comes up, and I have to choose one or more fields for
my primary key. How can I program this in VBA so that this step is done
automatically? If it helps, this is the line of code I use to link the table:
DoCmd.TransferDatabase acLink, "ODBC", strCon, acTable, "AttendanceCode",
"dbo_AttendanceCode", , True
You need to use the CreateTableDef method:
Public Function TableLinkODBC(ASourceName As String, _
Optional ADestinationName As String = "", _
Optional APrimaryKey As String = "") _
As Boolean
On Local Error GoTo LocalError
' Here you need your strCon.
Const CONNECTION_ODBC As String = "ODBC;" & _
"DRIVER={SQL Server};" & _
"SERVER=yourServer;" & _
"DATABASE=yourDatabase;" & _
"UID=user;" & _
"PWD=password"
TableLinkODBC = False
ASourceName = UCase(ASourceName)
If ADestinationName = "" Then
ADestinationName = ASourceName
End If
If TableExists(ADestinationName) Then
Debug.Print "-";
CurrentDbC.TableDefs.Delete ADestinationName
End If
Debug.Print "+"; ASourceName; "="; ADestinationName
CurrentDbC.TableDefs.Append _
CurrentDbC.CreateTableDef( _
ADestinationName, 0, ASourceName, CONNECTION_ODBC)
CurrentDbC.TableDefs.Refresh
If APrimaryKey <> "" Then
SQLExecute "CREATE INDEX pk_" & ADestinationName & " ON " & _
ADestinationName & "(" & APrimaryKey & ") WITH PRIMARY;"
End If
TableLinkODBC = True
Exit Function
LocalError:
MsgBox Err.Description
End Function
CurrentDbC is Michael Kaplan's solution, e.g.
http://access.joposol.com/accept/vba-makros/currentdb.html
TableExists() is a simple function checking the TableDefs collection, if
this table exists.
Specify the primary key fields as comma separated list, if the are not
automatically recognized.
mfG
--> stefan <--