Results 1 to 4 of 4

Thread: Connecting to Access

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7

    Connecting to Access

    I'm using an access 2000 and I need to get information from the database to VB, I used the data control, to display employee last name in a combo box, it only shows the first last and nothing else, how can get the whole list in the combo box to choose from?

    Thank you for your help

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi adnane,
    first of all, never use the bound datacontrol. Its evil. Learn how to do it the proper way, using ADO

    sample

    VB Code:
    1. Public Const strJetProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myaccess.mdb"
    2.  
    3.  
    4. 'Usage
    5.  
    6.  '* Place on a form a combo box and a command button
    7.  
    8. Public Sub Command1_Click()
    9.  
    10.     Dim MyConn As New ADODB.Connection
    11.     Dim MyRst As New ADODB.Recordset
    12.     Dim strTemp As String
    13.  
    14.  
    15.     With MyConn
    16.         .ConnectionString = strJetProvider
    17.         .Open
    18.     End With
    19.     With Me
    20.         .Combo1.Clear
    21.     End With
    22.     MyRst.Open "SELECT * FROM SQL", MyConn, adLockReadOnly, adLockOptimistic, adCmdText
    23.  
    24.     If MyRst.RecordCount <= 0 Then
    25.         MyRst.Close
    26.         MyConn.Close
    27.         Set MyRst = Nothing
    28.         Set MyConn = Nothing
    29.         strTemp = MsgBox("Sorry No Records are located?", vbOKOnly + vbCritical, "No Records Found")
    30.         Exit Sub
    31.     End If
    32.     Do While Not MyRst.EOF
    33.         If Not IsNull(MyRst("Field")) Then
    34.             Combo1.AddItem MyRst("Field")
    35.         End If
    36.         MyRst.MoveNext
    37.     Loop
    38.     MyRst.Close
    39.     If Combo1.ListCount > 0 Then
    40.         Combo1.ListIndex = 0
    41.     End If
    42.  
    43. End Sub

    you have to learn some more in order to understand, but once you get going you will see its the right thing to do.

    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    -= a peet post =-

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7
    Thanks peet for your reply, I appreciate it, I will use the ADO instead.
    I have one more question, I need to select an item from the combo box and display some information in some text boxes, when you click on the button, it'll save the information to the database.

    Thanks again for your help

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