Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2005
    Posts
    7

    Unanswered: Problem:how get fields to varaibles in vb6

    hi,
    I use VB6. connect Access 2002 with ADO2.5.
    purpose
    1.get fields1 of each records to combo
    2.get another field in variables

    problem
    1.when run program, first time, variable is not load value follow by default combo s item. When I click combo its just get value.
    2.I click combo to choose another but value of variable is still same first time click, its not change follow item which I click. I would like variables changes value follow item in combo,How?

    [vbcode]

    Private cn As ADODB.Connection
    Private rs As ADODB.Recordset
    Private Sub Form_Load()
    Set cn = New ADODB.Connection
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source= c:\mydb.mdb"
    cn.Open
    Set rs = New ADODB.Recordset
    rs.Open "chemical", cn, adOpenKeyset, adLockPessimistic, adCmdTable
    rs.MoveFirst
    Do Until rs.EOF = True I put each records of field1 in combo
    Combo1.AddItem rs.Fields("field1")
    rs.MoveNext 'moves next record
    Loop
    rs.MoveFirst
    End Sub

    Private Sub Combo1_Click()
    If rs.RecordCount <> 0 Then
    With rs
    Var1 = !field2
    Var2 = !field3
    Var3 = !field4
    End With
    Else
    MsgBox "No records found"
    End If
    End Sub

    [/vbcode]

  2. #2
    Join Date
    Dec 2004
    Location
    Laguna, Philippines
    Posts
    147
    Private Var1 as Variant
    Private Var2 as Variant
    Private Var3 as variant
    Private cn As ADODB.Connection
    Private rs As ADODB.Recordset

    Private Sub PutRecords
    If Not rs.Eof Then
    With rs
    Var1 = !field2
    Var2 = !field3
    Var3 = !field4
    End With
    Else
    MsgBox "No records found"
    End If
    end sub

    Private Sub Form_Load()
    Set cn = New ADODB.Connection
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source= c:\mydb.mdb"
    cn.Open
    Set rs = New ADODB.Recordset
    rs.Open "chemical", cn, adOpenKeyset, adLockPessimistic, adCmdTable
    rs.MoveFirst
    Do Until rs.EOF = True I put each records of field1 in combo
    Combo1.AddItem rs.Fields("field1")
    rs.MoveNext 'moves next record
    Loop
    rs.MoveFirst

    PutRecord '--> Call this procedure so that it will automatically get values during load
    End Sub

    Private Sub Combo1_Click()
    rs.Find "field1 = '" & combo1 & "'" 'Search the value.

    PutRecord '--> Call this procedure so that it will automatically get values during load
    End Sub

    [/vbcode]


    next time please be more clear on your english...
    Last edited by coleone; 02-09-05 at 04:26.
    Give me a place to code and i shall move the earth!

  3. #3
    Join Date
    Jan 2005
    Posts
    7

    Question Your advice is work but i still have some problem?

    hi,
    i' m so pleasure for your advice , it's pretty good, i can put field in variable but some problem occure when i click last item of combo, it's apper error "No record found" and value of that field do not put in variable, it 's same last value. How i solve this problem

    And if i would like to add Database via ADODC by create new form in order to add new data on DB. How i can put new record in combo and send value of that field in variable.

    Thank a lot



    Private Sub cmdAdd_Click()
    Adodc1.Recordset.AddNew
    End Sub

    Private Sub cmdDelete_Click()
    Adodc1.Recordset.Delete
    End Sub

    Private Sub cmdok_Click()
    Unload Me
    End Sub

    Private Sub cmdupdate_Click()
    Adodc1.Recordset.Update
    End Sub

  4. #4
    Join Date
    Dec 2004
    Location
    Laguna, Philippines
    Posts
    147
    Private Sub Combo1_Click()
    rs.MoveFirst '--> I forgot to include it on the code, maybe it is the source of your current error.

    rs.Find "field1 = '" & combo1 & "'" 'Search the value.

    PutRecord '--> Call this procedure so that it will automatically get values during load
    End Sub

    on your next question...

    Private Sub cmdupdate_Click()
    Adodc1.Recordset.Update

    Combo1.AddItem rs.Fields("field1") '--> Add this, but make sure your event is always addnew...

    PutRecord '--> Call this procedure again.
    End Sub
    Give me a place to code and i shall move the earth!

  5. #5
    Join Date
    Jan 2005
    Posts
    7

    Talking Thank your

    Thank your, for your kindness

    I will try again

Posting Permissions

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