Solved

Create ODBC via VBA for access

Posted on 2010-08-24
6
768 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

773 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