Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Configure Access Database ODBC using VB6 Code

Posted on 2003-12-07
12
Medium Priority
?
60,524 Views
Last Modified: 2013-12-25
Right now I have an application that uses an Access database and an ODBC connection using VB6.

I currently have to set up the ODBC configuration using Control Panel/ Administrative Tools/Data Sources (ODBC)/User DSN etc in Windows 2000.

This means that when I distribute the application I will have to get other users to go into Control Panel, and configure the ODBC and set the DSN for the proper database.

Has anyone figured out a way to do this from within VB6. It sounds like an API call(?) will be required...

I would like to have the user select the database and then have VB configure the DSN name and driver using code...
0
Comment
Question by:DrD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 8

Expert Comment

by:MYLim
ID: 9894817
Try This:

Option Explicit


Private Const ODBC_ADD_DSN = 1        ' Add data source
Private Const ODBC_CONFIG_DSN = 2     ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3     ' Remove data source
Private Const ODBC_ADD_SYS_DSN = 4    ' Add a new system data source.

'ODBC_CONFIG_SYS_DSN: Modify an existing system data source.

'ODBC_REMOVE_SYS_DSN: Remove an existing system data source

Private Const vbAPINull As Long = 0&  ' NULL Pointer
       
     
       
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long



Public Function AddODBCDSN()

    Dim strDriver As String
    Dim strAttributes As String
    Dim intRet As Long
     
   'since i do not have SQL Server installed....
    strDriver = "Microsoft Access Driver (*.mdb)"      'Set the attributes delimited by null.
    'See driver documentation for a complete
    'list of supported attributes.
     
   'You should use this
    'strDriver = "SQL Server"
    strAttributes = "SERVER=SomeServer" & Chr$(0)
    strAttributes = strAttributes & "DESCRIPTION=Test DSN" & Chr$(0)
    strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
    strAttributes = strAttributes & "DATABASE=c:\test.mdb" & Chr$(0)
    'strAttributes = strAttributes & "UID=" & Chr$(0)
    'strAttributes = strAttributes & "PWD=" & Chr$(0)
       
   'To show dialog, use Form1.Hwnd instead of vbAPINull.
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, strDriver, strAttributes)
    If intRet Then
        MsgBox "DSN Created"
    Else
        MsgBox "Create Failed"
     
   End If
End Function

0
 
LVL 8

Expert Comment

by:MYLim
ID: 9894822
0
 
LVL 8

Expert Comment

by:MYLim
ID: 9894827
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
MYLim earned 2000 total points
ID: 9894837
Step-by-Step Example
=====================
1.Start a New Project.
2.In the Advanced tab of the Options dialog box under the Tools menu, set a Conditional Compilation Argument named WIN32 equal to 1 if using Visual Basic 4.0 32-bit, or 0 if using Visual Basic 4.0 16-bit.
3.Add two CommandButtons to the default form.
4.Add the following code to the General Declarations:      Option Explicit

      'Constant Declaration
      Private Const ODBC_ADD_DSN = 1        ' Add data source
      Private Const ODBC_CONFIG_DSN = 2     ' Configure (edit) data source
      Private Const ODBC_REMOVE_DSN = 3     ' Remove data source
      Private Const vbAPINull As Long = 0&  ' NULL Pointer

      'Function Declare
      #If WIN32 Then

          Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
          (ByVal hwndParent As Long, ByVal fRequest As Long, _
          ByVal lpszDriver As String, ByVal lpszAttributes As String) _
          As Long
      #Else
          Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" _
          (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal _
          lpszDriver As String, ByVal lpszAttributes As String) As Integer
      #End If
                              
5.Add the following code into the Click event of Command1:      #If WIN32 Then
          Dim intRet As Long
      #Else
          Dim intRet As Integer
      #End If
      Dim strDriver As String
      Dim strAttributes As String

      'Set the driver to SQL Server because it is most common.
      strDriver = "SQL Server"
      'Set the attributes delimited by null.
      'See driver documentation for a complete
      'list of supported attributes.
      strAttributes = "SERVER=SomeServer" & Chr$(0)
      strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
      strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
      strAttributes = strAttributes & "DATABASE=pubs" & Chr$(0)
      'To show dialog, use Form1.Hwnd instead of vbAPINull.
      intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
      strDriver, strAttributes)
      If intRet Then
          MsgBox "DSN Created"
      Else
          MsgBox "Create Failed"
      End If
                              
6.Add the following code into the Click event of Command2:      #If WIN32 Then
          Dim intRet As Long
      #Else
          Dim intRet As Integer
      #End If
      Dim strDriver As String
      Dim strAttributes As String

      'Set the driver to SQL Server because most common.
      strDriver = "SQL Server"
      'Set the attributes delimited by null.
      'See driver documentation for a complete list of attributes.
      strAttributes = "DSN=DSN_TEMP" & Chr$(0)
      'To show dialog, use Form1.Hwnd instead of vbAPINull.
      intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _
      strDriver, strAttributes)
      If intRet Then
          MsgBox "DSN Deleted"
      Else
          MsgBox "Delete Failed"
      End If
                              
7.Run the project.
8.Click Command1 to add a DSN named DSN_TEMP.
9.Click Command2 to remove the DSN named DSN_TEMP.

'that all
0
 
LVL 8

Expert Comment

by:MYLim
ID: 9894934
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 9894938
Hi DrD,

Have you considered using a dsnless connection?

Invoke browse for file dialog, then build the connect string and open the connection
http://www.able-consulting.com/MDAC/ADO/Connection/ODBC_DSNLess.htm#ODBCDriverForAccess

Alan :)
0
 
LVL 8

Expert Comment

by:spongie
ID: 9898123
Hi DrD. I think you only need to create the dsn once and that's during installation. So, try creating this with your setup program. I suggest using Inno Setup. It's free.

http://www.jrsoftware.org/isinfo.php

Visit the vb tutorial here:

http://www.jrsoftware.org/iskb.php?vb

or just run this software to create the packaging script:

http://www.randem.com/innoscript/innoscript.htm

and use this tool to create the script for your DSN:

http://www.istool.org/

It might not be the quick and easy way but when you start getting PDW problems, you'll end up using other installation softwares like the ones above. But then you could always find out for yourself...

Good Luck! :0)
0
 

Author Comment

by:DrD
ID: 9898683
Wow!

I sure got some responses to this one.. I'll get back as soon as I can. It will take me a day or so to work through all of them...

Thanks!

David (drD)

0
 

Expert Comment

by:Sycon1
ID: 10313142
I need to know ehat lines of code i need to connect to a microsoft access databse, then how to add records to the databse.thank you
0
 

Expert Comment

by:stromberg45
ID: 11725065
this code works, well it puts up the DSN entry, but it will not select the database at all. that is, it puts up a DSN that points to nothing

my parameters are as follows --

      strDriver = "Microsoft Access Driver (*.mdb)"
      strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
      strAttributes = strAttributes & "DSN=RM TEMP" & Chr$(0)
      strAttributes = strAttributes & "DATABASE=" & App.Path & "RM.mdb" & Chr$(0)

      intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)

anyone help?
0
 

Expert Comment

by:mrjdesign
ID: 13745767
is there one thing missing here???

strAttributes = strAttributes & "DATABASE=" & App.Path & "RM.mdb" & Chr$(0)

shouldnt that be...

strAttributes = strAttributes & "DATABASE=" & App.Path & "\RM.mdb" & Chr$(0)

The backslash isn't added by default to the app.path
0
 

Expert Comment

by:amitmahajan
ID: 14185977
This works -

strAttributes = strAttributes & "Dbq=" & App.Path & "\RM.mdb" & Chr$(0)
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

688 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