Solved

Create ODBC via VBA for access

Posted on 2010-08-24
6
765 Views
Last Modified: 2013-11-27
I have an access front end that references a SQL backend.  I have the
ODBC connection connecting the front end to the back end set up on my pc and it works fine.  

My problem occurs when I distribute the front end to another user with out the ODBC connection
set up on their PC.  They recieve an error.  I have to distribute to a number of users but I do
not want them to set up the ODBC connection since that task is error prone and the users and not
all savy with computers.

how can I use VBA to set up the connection? Please use examples in your code.
0
Comment
Question by:RDLFC
6 Comments
 
LVL 4

Accepted Solution

by:
Clothahump earned 167 total points
ID: 33516898
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset

cnn.Open "YourDSN", "YourUsername", "YourPassword"
rs.ActiveConnection = cnn
rs.CursorLocation = adUseServer

rs.Source = "SELECT * FROM TABLE WHERE FIELD='data'"
rs.Open
' code goes here to manipulate the recordset retrieved
rs.Close

Set rs = Nothing
Set cnn = Nothing


If you don't have a DSN established, look at www.ConnectionStrings.com and find a connection string that fits your needs to use in the cnn.open line.
0
 

Author Comment

by:RDLFC
ID: 33517796
thanks i will check out your solution when I get to work tomorrow.
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 167 total points
ID: 33518807
Here is a full module with all functions for performing this.
Copy and paste into a new module and omit the first line:
Attribute VB_Name = "basWinAPI_DSN"

Study the in-line comments for typical usage and examples.
Note that the Private functions are for internal use - by the Public functions.

Be aware, that limited user rights may prohibit creation of DSN entries.

/gustav
basWinAPI-DSN.txt
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 166 total points
ID: 33518851
I suggest

the result is

rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic
rst.LockType = adLockBatchOptimistic
should be better and able to reconnect to database several time
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset


Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset


cnn.Open "dsn", "user", "pwd"
rst.ActiveConnection = cnn
rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic
rst.LockType = adLockBatchOptimistic

rst.Source = "SELECT * FROM yourTable  "
rst.Open



rst.Close
cnn.Close

Set rst = Nothing
Set cnn = Nothing

Open in new window

0
 

Author Comment

by:RDLFC
ID: 33552606
thanks guys for your input, but I used the article at the site below and found it to be very helpful.

http://support.microsoft.com/kb/892490
0
 

Author Closing Comment

by:RDLFC
ID: 33552611
The comments assisted me in gathering an understanding but the article assisted in implementing the DNS less connection.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now