Results 1 to 22 of 22

Thread: Connecting an Access database to VB6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Connecting an Access database to VB6

    Hey

    -Using visual basic 6 to connect to a Microsoft Access 2000 database
    - "data" i think its called a data control not completely sure.

    I'm quite new to visual basic 6 and would like some help designing a program.

    I want to connect a combo box to a databae, for example if the user clicks on the combo box the names of the tables should appear. When the user clicks on the table name its attributes should be displayed in a list box below the combo box.

    Can anyone help write the code to accomplish this

    Thank you
    Last edited by tyrone8888; Feb 20th, 2007 at 11:07 AM. Reason: Add more information

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Connecting an Access database to VB6

    How are you connecting to the database in the first place?

    Are you using ADO code or a data bound control?

    (Duplicate thread in Classic VB deleted. Please do not post the same question in multiple forum sections.)

  3. #3
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Connecting an Access database to VB6

    The easiest way and of course most despised way by the hard core coders.
    but since you are new this is at least the best way to experiment for now.
    is using a ADODC controle to bind to a table in your database. (ADO data controle)
    you can link the combobox to a specific field of this table.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Quote Originally Posted by Hack
    How are you connecting to the database in the first place?

    Are you using ADO code or a data bound control?

    (Duplicate thread in Classic VB deleted. Please do not post the same question in multiple forum sections.)

    No to sure what you mean by ADO, but the database that i want the program to connect to is Microsoft Access. I'm using the "DATA" from the tool bar. I've used the following code to connect the combo box to the database but i can not select the tables that are displayed

    Private Sub Form_Activate()
    Dim strTableList As String

    'In database if there are any tables that contain data add to
    ' the combo box

    For Each t In datControl.Database.TableDefs
    If t.Attributes = 0 Then
    cboTables.AddItem (t.Name)
    End If
    Next
    strTableList = strTableList & lstFields

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Connecting an Access database to VB6

    Set a reference to the Microsoft ActiveX Data Objects Library

    Paste this into a test project with one form and a Listbox on the form
    VB Code:
    1. Option Explicit
    2.  
    3. Private Cn As ADODB.Connection
    4. Private rstSchema As ADODB.Recordset
    5. Private strCn As String
    6.  
    7. Private Sub OpenDB()
    8.      
    9.    Set Cn = New ADODB.Connection
    10.    strCn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    11.         "Data Source=c:\yourMDB.mdb;" & _
    12.         "Persist Security Info=False"
    13.  
    14.    Cn.Open strCn
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18. OpenDb      
    19.    Set rstSchema = Cn.OpenSchema(adSchemaTables)
    20.  
    21.  Do Until rstSchema.EOF
    22.       List1.AddItem "Table name: " & rstSchema!TABLE_NAME & " " & "Table type: " & rstSchema!TABLE_TYPE
    23.       rstSchema.MoveNext
    24.    Loop
    25.    
    26.    ' clean up
    27.    rstSchema.Close
    28.    Cn.Close
    29.    Set rstSchema = Nothing
    30.    Set Cn = Nothing
    31. End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    It is required that i use a DAO data control instead of the Microsoft ActiveX Data Objects Library. The code below connects the database to the combo box
    Private Sub Form_Activate()
    Dim strTableList As String
    For Each t In datControl.Database.TableDefs
    If t.Attributes = 0 Then
    cboTables.AddItem (t.Name)
    End If
    Next

    End Sub

    The problem that i experiencing is allowing the user to select the table and to get it fields to be displayed in a list box

    I've attempted to solve this problem by creating a command button , so when a table is selected from the combo box by pressing the command button will show the its fields
    Heres the code
    If Len(cboTables.Text) <> 0 Then
    strCurrentTable = cboTables.Text
    MsgBox strCurrentTable
    datControl.RecordSource = "SELECT * from " & strCurrentTable
    End If

    Can anyone help me to change this code , so that it appears in the list box instead of a message?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    Quote Originally Posted by tyrone8888
    It is required that i use a DAO data control instead of the Microsoft ActiveX Data Objects Library.
    Is there a very good reason for that?

    DAO is obsolete, and will not work on many modern computers.. if you look at the VB6 help for that control (published in 1998), it explicitly says to not use it. Unless you have no option whatsoever, do not even consider using DAO.

    If you want to be in control of what your program does, do not use a data control either - for the reasons see the "bound controls" link in Hacks signature, or the similar one in our Database FAQ's.


    See the ADO Tutorial link in my signature for the ADO code equivalent of what you are doing - it's not that complex!

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Quote Originally Posted by si_the_geek
    Is there a very good reason for that?

    DAO is obsolete, and will not work on many modern computers.. if you look at the VB6 help for that control (published in 1998), it explicitly says to not use it. Unless you have no option whatsoever, do not even consider using DAO.

    If you want to be in control of what your program does, do not use a data control either - for the reasons see the "bound controls" link in Hacks signature, or the similar one in our Database FAQ's.


    See the ADO Tutorial link in my signature for the ADO code equivalent of what you are doing - it's not that complex!
    Thanks alot just read the "Why is using bound controls a bad thing" how do i install or gain access to "Microsoft ActiveX Data Objects Library" Do i need to go to Project>Components?

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    Close.. Components is for controls, you need to go to Project->References, and select "Microsoft ActiveX Data Objects 2.x library".

    For an explanation of how to use it, see the tutorial.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Problem solved
    Last edited by tyrone8888; Feb 22nd, 2007 at 12:34 PM. Reason: Solved the problem

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Connecting an Access database to VB6

    1) Please, please, please use [vbcode][/Highlight] tags around your code... it preserves the indenting and makes things easier to read.
    2) .... Where do you get the error? There's a a nasty rumor flying about that we can read minds, but that's simply not true... well, most of us can't there might be one or two that can.

    Once we know where the problem is, we can more acuratly answer the question.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Quote Originally Posted by Hack
    Set a reference to the Microsoft ActiveX Data Objects Library

    Paste this into a test project with one form and a Listbox on the form
    VB Code:
    1. Option Explicit
    2.  
    3. Private Cn As ADODB.Connection
    4. Private rstSchema As ADODB.Recordset
    5. Private strCn As String
    6.  
    7. Private Sub OpenDB()
    8.      
    9.    Set Cn = New ADODB.Connection
    10.    strCn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    11.         "Data Source=c:\yourMDB.mdb;" & _
    12.         "Persist Security Info=False"
    13.  
    14.    Cn.Open strCn
    15. End Sub
    16.  
    17. Private Sub Form_Load()
    18. OpenDb      
    19.    Set rstSchema = Cn.OpenSchema(adSchemaTables)
    20.  
    21.  Do Until rstSchema.EOF
    22.       List1.AddItem "Table name: " & rstSchema!TABLE_NAME & " " & "Table type: " & rstSchema!TABLE_TYPE
    23.       rstSchema.MoveNext
    24.    Loop
    25.    
    26.    ' clean up
    27.    rstSchema.Close
    28.    Cn.Close
    29.    Set rstSchema = Nothing
    30.    Set Cn = Nothing
    31. End Sub
    Been through the tutorial which as been extremely helpful. your code works perfectly fine apart from that I want the user to be able to select a table name from a combo(which is connected to the database) For example student, course, etc. Then when the user clicks on the student name I want the attributes to be shown in the list box. For example iDstu, Name, Surname etc. How can i adjust your code to reflect this?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    the code below is my attempt to add the name of the table to the combo box

    Private Sub Form_Activate()
    'Dim strTableList As String
    'Set rstSchema = Cn.OpenSchema(adSchemaTables)

    Set Cn = New ADODB.Connection ' We've declared it as a ADODB connection lets set it
    ' This is the connection string - is setting the provider to the Jet Engine and version 4.0.
    Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source = c:\my documents\newResults.mdb"

    cboTables.AddItem "Table name: " & rstSchema!TABLE_NAME & " " & "Table type: " & rstSchema!TABLE_TYPE
    rstSchema.MoveNext

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    I presume that just adds one item to the list - to add multiple you need to use a Do Until .. Loop like in Hack's example.

    ..and as TG said, when you post code please put it inside VBCode tags so it is displayed in a more readable way - either using the button in the post editor screen (or at the top of the Quick Reply box), or by putting them in manually, like this: [vbcode] 'code here [/vbcode]

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Here's my attempt to to display the table names in the combo box, this works perfectly fine apart from that it display unnecessary information in the combo such as MSys
    Access Objects, MSysACEs,MSysObjects. All i want is thecombo box to display these three tables which are Attainment,Student Module.

    [Highlight=VB]Option Explicit

    Private Cn As ADODB.Connection ' This is the connection
    Private rstSchema As ADODB.Recordset ' This is the recordset
    Private strCn As String ' string to be used in the code
    Public strSelectedItem As String
    VB Code:
    1. Private Sub OpenDB()
    2.    Set Cn = New ADODB.Connection  'Declared it as a ADODB connection
    3.    strCn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    4.         "Data Source=c:\Documents and Settings\newResults.mdb"  'A connection string that is connected to the database
    5.    Cn.Open strCn
    6. End Sub
    7. Private Sub Form_Load()
    8. ' retrieve all the information from the database and places it in the combobox
    9. OpenDB
    10.    Set rstSchema = Cn.OpenSchema(adSchemaTables)
    11.  
    12.  Do Until rstSchema.EOF
    13.     cboTables.AddItem " " & rstSchema!TABLE_NAME
    14.       rstSchema.MoveNext
    15.    Loop
    16.  
    17. End Sub

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    if you look at the help for adSchemaTables/OpenSchema, you can see that the recordset that is returned contains several fields.. one of these is TABLE_TYPE, which can have several values including "Table" (an actual table) "View" (a view), and specifically for Access it can also be "Access Table" (one of the system tables).

    To ensure that you only get valid table names, only add items to the list if TABLE_TYPE is Table (or View if you want to allow that).


    Note that when you have finished with this recordset, you need to close it properly (as you should do with all recordsets & connections etc), eg:
    VB Code:
    1. rstSchema.Close
    2.   Set rstSchema = Nothing

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Quote Originally Posted by si_the_geek
    if you look at the help for adSchemaTables/OpenSchema, you can see that the recordset that is returned contains several fields.. one of these is TABLE_TYPE, which can have several values including "Table" (an actual table) "View" (a view), and specifically for Access it can also be "Access Table" (one of the system tables).

    To ensure that you only get valid table names, only add items to the list if TABLE_TYPE is Table (or View if you want to allow that).


    Note that when you have finished with this recordset, you need to close it properly (as you should do with all recordsets & connections etc), eg:
    VB Code:
    1. rstSchema.Close
    2.   Set rstSchema = Nothing
    I understand what your saying but i do not understand how to do this. Could you help me

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    Assuming you want just tables (and not Views too), you can change the loop to this:
    VB Code:
    1. Do Until rstSchema.EOF
    2.       If UCase(rstSchema.Fields("TABLE_TYPE").Value & "") = "TABLE" Then
    3.         cboTables.AddItem " " & rstSchema.Fields("TABLE_NAME").Value
    4.       End If
    5.       rstSchema.MoveNext
    6.    Loop
    7.  
    8.    rstSchema.Close    'close recordset
    9.    Set rstSchema  = Nothing
    10.  
    11.  
    12.    Cn.Close   'close connection (you probably want to move this to Form_Unload)
    13.    Set Cn = Nothing

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Thanks alot si_the_geek that works absolutely perfectly. Can you tell me how I can allow the user to select the table name from the combo box and its individual fields show in a list box? For example the user clicks the combo box a list of tables called department, employee appear. The user clicks department and its fields which are de_ID, Name and Room show in the list box. If the user clicks on employee the department fields are removed and replaced with the employee fields which are em_ID, Name, Surname and Address

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    You just do basically the same thing again... but with a different schema recordset, which has appropriate fields. That would be adSchemaColumns, which has (among others) TABLE_NAME and COLUMN_NAME.

    The IF statement simply needs to check that the Table_Name matches your selection.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: Connecting an Access database to VB6

    Quote Originally Posted by si_the_geek
    You just do basically the same thing again... but with a different schema recordset, which has appropriate fields. That would be adSchemaColumns, which has (among others) TABLE_NAME and COLUMN_NAME.

    The IF statement simply needs to check that the Table_Name matches your selection.
    Can you check if my attempt is right? also where do i place the code i was thinkin in the list box.

    ' retrieve all the information from the combobox and places it in the listbox

    VB Code:
    1. OpenDB
    2.    Set rstSchema = Cn.OpenSchema(adSchemaColumns)
    3.  
    4. Do Until rstSchema.EOF
    5.       If UCase(rstSchema.Fields("TABLE_TYPE").Value & "") = "cboTables" Then ' if the table type is equal to table
    6.         lstFields.AddItem " " & rstSchema.Fields("COLUMN_NAME").Value ' Add name of fields to the listbox
    7.       End If
    8.       rstSchema.MoveNext
    9.    Loop
    10.  
    11.    rstSchema.Close    'close recordset
    12.    Set rstSchema = Nothing
    13. End Sub
    Last edited by tyrone8888; Mar 1st, 2007 at 05:01 AM.

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Connecting an Access database to VB6

    That's good, but line 5 should be like this:
    Code:
    If UCase(rstSchema.Fields("TABLE_NAME").Value & "") = cboTables.Text Then
    Note that you don't need line 1 as the OpenDB sub should only be called once; Also, you should close the connection when the form closes.. to do that put the following into Form_Unload:
    vb Code:
    1. Cn.Close
    2. Set Cn = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width