I am currently developing an access application, which dynamically builds userforms through a VBA module. In order to create the forms, I need data from an MS SQL DB. I collect this data through linked table connections, which are established upon initiation of the entire application by using the AttachDSNlessTable method:
https://support.microsoft.com/en-us/kb/892490
The userinputs are collected in the userforms, and subsequently inserted into some other linked tables (:ResultTables) on the same sql-server.
MY PROBLEM IS that VB-based linked table connections does not let me insert my userform inputs to the resulttables. When I view the form in FormView, it is instead completely blank. All the controls are nonetheless visible in DesignView.
The problem doesn't exist when I manually create the linked tables, apparently because I can choose an index which then allows me to fill new rows in the table.
I am completely sure that the user has the right access to the sql-server to perform update/insert/delete procedures. I have tried to index the table on the server, but the index is not inherented in Access. Once the table is linked, I can't edit the connection. I have also tried the approaches suggested for blank userforms:
https://support.microsoft.com/en-us/kb/93261
Option Compare Database
'//Name : AttachDSNLessTable
'//Purpose : Create a linked table to SQL Server without using a DSN
'//Parameters
'// stLocalTableName: Name of the table that you are creating in the current database
'// stRemoteTableName: Name of the table that you are linking to on the SQL Server database
'// stServer: Name of the SQL Server that you are linking to
'// stDatabase: Name of the SQL Server database that you are linking to
'// stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'// stPassword: SQL Server user password
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
On Error GoTo AttachDSNLessTable_Err
Dim td As TableDef
Dim stConnect As String
For Each td In CurrentDb.TableDefs
If td.Name = stLocalTableName Then
CurrentDb.TableDefs.Delete stLocalTableName
End If
Next
If Len(stUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Else
'//WARNING: This will save the username and the password with the linked table information.
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
End If
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
CurrentDb.TableDefs.Refresh
'td.CreateIndex (ID2)
AttachDSNLessTable = True
Exit Function
AttachDSNLessTable_Err:
AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description
End Function