472,794 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

Linking external table with ODBC using code

kai
Hi, All
I used to link a SQL Server 2000 table through ODBC in Access 2003 using
Wizard, now because the security requirement, I want to do the same job
using code. I tried hard, but not successful. Please help.

Thanks

Kai


Nov 13 '05 #1
2 6003
kai wrote:
Hi, All
I used to link a SQL Server 2000 table through ODBC in Access 2003 using
Wizard, now because the security requirement, I want to do the same job
using code. I tried hard, but not successful. Please help.

Thanks

Kai


Code below, watch for wrapping...

--- begin cut here ---
Option Compare Database
Option Explicit
Private Declare Function basODBC_GetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long

Sub TestAttachODBC()
AttachODBC "myserver", "mydatabase", "myusername", "mypassword"
End Sub

Function AttachODBC(pstrServer As String, pstrDatabase As String,
pstrUser As String, pstrPassword As String)
' Generic Attach ODBC Tables from SQL Server
' T.Best 14 Nov 2004
' Uses DAO

Dim strSQLTableList As String
Dim dbSQL As DAO.Database
Dim dbLocal As DAO.Database
Dim rst As DAO.Recordset
Dim tdf As DAO.TableDef
Dim strConnect As String
Dim fDropped As Boolean
Dim lngNumTables As Long
Dim lngTable As Long

' SQL Select for getting list of tables
strSQLTableList = "select name from sysobjects where
objectProperty(id,'IsUserTable')=1 " & _
"or objectproperty(id,'IsView')=1"

' Connection string
strConnect = "ODBC;DRIVER={SQL Server}" & _
";SERVER=" & pstrServer & _
";APP=MyApp" & _
";WSID=" & MachineName() & _
";DATABASE=" & pstrDatabase & _
";Address=" & pstrServer & ",1433" & _
";Trusted_Connection=No" & _
";UID=" & pstrUser & _
";PWD=" & pstrPassword & _
";DSN="

Debug.Print strConnect

' open the SQL database
Set dbSQL = DBEngine(0).OpenDatabase("", dbDriverCompleteRequired,
False, strConnect)
Set dbLocal = CurrentDb()

' Drop old ODBC tables
' Note: This may need customising in hetrogeneous environment :-)
Do
' one simple loop will fail to unattach all tables
' so we'll redo the unattach until nothing gets
' unattached.
fDropped = False
For Each tdf In dbLocal.TableDefs
If Len(tdf.Connect) Then

'Debug.Print "Dropping Linked Table " & tdf.Name
dbLocal.TableDefs.Delete tdf.Name
fDropped = True
End If
Next
Set tdf = Nothing
If Not fDropped Then
' if nothing dropped then we've unattached all the tables
Exit Do
End If
Loop

' list tables
Set rst = dbSQL.OpenRecordset(strSQLTableList, dbOpenDynaset,
dbSeeChanges + dbReadOnly + dbSQLPassThrough)
With rst
If .RecordCount Then
.MoveLast
.MoveFirst
lngNumTables = .RecordCount
SysCmd acSysCmdInitMeter, "Attaching Tables", lngNumTables
Do Until .EOF
lngTable = lngTable + 1
SysCmd acSysCmdUpdateMeter, lngTable
Set tdf = dbLocal.CreateTableDef(.Fields("Name"))
tdf.Connect = strConnect
tdf.SourceTableName = .Fields("Name")
dbLocal.TableDefs.Append tdf
Set tdf = Nothing
.MoveNext
Loop
SysCmd acSysCmdRemoveMeter
End If
.Close
End With
Set rst = Nothing
dbSQL.Close
Set dbSQL = Nothing
Set dbLocal = Nothing

End Function
Public Function MachineName() As String
Dim lngNameSize As Long
Dim lngX As Long
Dim strBuffer As String

strBuffer = Space$(255)
lngNameSize = Len(strBuffer)
lngX = basODBC_GetComputerName(strBuffer, lngNameSize)
MachineName = Left$(strBuffer, lngNameSize)

End Function
--- end cut here ---
--
This sig left intentionally blank
Nov 13 '05 #2
kai
Thanks Trevor.

Kai
"Trevor Best" <no****@besty.org.uk> wrote in message
news:41***********************@news.zen.co.uk...
kai wrote:
Hi, All
I used to link a SQL Server 2000 table through ODBC in Access 2003
using Wizard, now because the security requirement, I want to do the same
job using code. I tried hard, but not successful. Please help.

Thanks

Kai


Code below, watch for wrapping...

--- begin cut here ---
Option Compare Database
Option Explicit
Private Declare Function basODBC_GetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long

Sub TestAttachODBC()
AttachODBC "myserver", "mydatabase", "myusername", "mypassword"
End Sub

Function AttachODBC(pstrServer As String, pstrDatabase As String, pstrUser
As String, pstrPassword As String)
' Generic Attach ODBC Tables from SQL Server
' T.Best 14 Nov 2004
' Uses DAO

Dim strSQLTableList As String
Dim dbSQL As DAO.Database
Dim dbLocal As DAO.Database
Dim rst As DAO.Recordset
Dim tdf As DAO.TableDef
Dim strConnect As String
Dim fDropped As Boolean
Dim lngNumTables As Long
Dim lngTable As Long

' SQL Select for getting list of tables
strSQLTableList = "select name from sysobjects where
objectProperty(id,'IsUserTable')=1 " & _
"or objectproperty(id,'IsView')=1"

' Connection string
strConnect = "ODBC;DRIVER={SQL Server}" & _
";SERVER=" & pstrServer & _
";APP=MyApp" & _
";WSID=" & MachineName() & _
";DATABASE=" & pstrDatabase & _
";Address=" & pstrServer & ",1433" & _
";Trusted_Connection=No" & _
";UID=" & pstrUser & _
";PWD=" & pstrPassword & _
";DSN="

Debug.Print strConnect

' open the SQL database
Set dbSQL = DBEngine(0).OpenDatabase("", dbDriverCompleteRequired,
False, strConnect)
Set dbLocal = CurrentDb()

' Drop old ODBC tables
' Note: This may need customising in hetrogeneous environment :-)
Do
' one simple loop will fail to unattach all tables
' so we'll redo the unattach until nothing gets
' unattached.
fDropped = False
For Each tdf In dbLocal.TableDefs
If Len(tdf.Connect) Then

'Debug.Print "Dropping Linked Table " & tdf.Name
dbLocal.TableDefs.Delete tdf.Name
fDropped = True
End If
Next
Set tdf = Nothing
If Not fDropped Then
' if nothing dropped then we've unattached all the tables
Exit Do
End If
Loop

' list tables
Set rst = dbSQL.OpenRecordset(strSQLTableList, dbOpenDynaset,
dbSeeChanges + dbReadOnly + dbSQLPassThrough)
With rst
If .RecordCount Then
.MoveLast
.MoveFirst
lngNumTables = .RecordCount
SysCmd acSysCmdInitMeter, "Attaching Tables", lngNumTables
Do Until .EOF
lngTable = lngTable + 1
SysCmd acSysCmdUpdateMeter, lngTable
Set tdf = dbLocal.CreateTableDef(.Fields("Name"))
tdf.Connect = strConnect
tdf.SourceTableName = .Fields("Name")
dbLocal.TableDefs.Append tdf
Set tdf = Nothing
.MoveNext
Loop
SysCmd acSysCmdRemoveMeter
End If
.Close
End With
Set rst = Nothing
dbSQL.Close
Set dbSQL = Nothing
Set dbLocal = Nothing

End Function
Public Function MachineName() As String
Dim lngNameSize As Long
Dim lngX As Long
Dim strBuffer As String

strBuffer = Space$(255)
lngNameSize = Len(strBuffer)
lngX = basODBC_GetComputerName(strBuffer, lngNameSize)
MachineName = Left$(strBuffer, lngNameSize)

End Function
--- end cut here ---
--
This sig left intentionally blank

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard Muller | last post by:
Hi All, I've got the ASP script shown below that complains as follows: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) Data source name not found and no default driver specified...
18
by: Mark P | last post by:
I have a bit of vb code that uses Tables.Append to programatically link tables from Oracle and DB2 datasources. The problem I am having on some client machines is that the link will take anywhere...
2
by: Simon Bitchenor | last post by:
I am linking an Access 97 front-end database to an ODBC data source (Sage Line 50 Version 9). Running the link on a Windows 98 PC the performance of the linked table is instant. Running under...
2
by: Michael | last post by:
High there everyone, I have inherited a database with an odbc table (linking to an oracle database) , we have recently updated our operating environment to windows 2000 (access 2000). since...
0
by: Salad | last post by:
My operating system is WinXP Pro. I have 1 gig of memory and the chip is an Intel Pentium 4. I am using Access97. A table has been supplied to me from an external source createdc from Visual...
1
by: ramazan_ozturk | last post by:
I have the following problem with Access 2003, hopefully someone can help. I'm using MS Office Pro. 2003 on a Windows XP Pro. SP2 machine. When I try to get external data import/link a table I...
0
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when...
0
by: dotyet | last post by:
Hi Everyone, I am trying to build a DB2 UDB UDF which can perform regex over the table data. The user defined function will call an external .dll file to do the task. I am referring to the...
7
by: Salad | last post by:
I am converting an application from A97 to A2003. I have 2 tables created by another application as a Foxpro.dbf. The table has no index. The connect string in A97 is FoxPro...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.