mcgrcoAgain
Registered User.
- Local time
- Today, 23:38
- Joined
- Jan 26, 2006
- Messages
- 47
hI. I want to connect msaccess to a sql server database using a dns less connection. I dont want each user to have to set up a system DNS as they all should have acceess to the sql server. The problem I have is that using the following code the data types are differnt between the sql server and the access database. doubles are not text in the database. I was to run this script on a large number of tables so I dont want to try to change the data types on tghe fly. Any suggestion on how this can be fixed or a better way of foing it. Many thanks
Code is below
Function AttachDSNLessTable(stLocalTableName As String, stServer As String, stDatabase As String, stRemoteTableName 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
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function
AttachDSNLessTable_Err:
If err.Number = 3011 Then
MsgBox stRemoteTableName & " does not exist for the date selected: ", vbCritical
Else
MsgBox "AttachDSNLessTable encountered an unexpected error: " & err.Description
AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error: " & err.Description
End If
AttachDSNLessTable = False
Set rs = Nothing
Set db = Nothing
End Function
Code is below
Function AttachDSNLessTable(stLocalTableName As String, stServer As String, stDatabase As String, stRemoteTableName 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
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function
AttachDSNLessTable_Err:
If err.Number = 3011 Then
MsgBox stRemoteTableName & " does not exist for the date selected: ", vbCritical
Else
MsgBox "AttachDSNLessTable encountered an unexpected error: " & err.Description
AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error: " & err.Description
End If
AttachDSNLessTable = False
Set rs = Nothing
Set db = Nothing
End Function