7 Replies - 4988 Views - Last Post: 25 October 2012 - 02:29 PM Rate Topic: -----

#1 madujr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 12

Passing vb6 option button value to an access field

Posted 23 October 2012 - 08:22 AM

Hi everyone.
I need to find a way to pass the value of my vb6 option button to an access database field. Let me describe it here:

I have this field called Gender in my database, which is a Text data type. It should hold either "Male" or "Female" value. This "Male" or "Female" value shall be coming from my vb6 form using option buttons. Option1 holds the Male value and Option2 holds the Female value. When the user clicks either option button, it should pass its value to the Gender field of my database.

Here's my code so far:
 
Private Sub Command1_Click()
Dim myRS As Recordset
    If Option1.Value = True Then
        myRS.Fields!Gender = "Male"
    ElseIf Option2.Value = True Then
        myRS.Fields!Gender = "Female"
    End If
    
Adodc1.Recordset.Update
End Sub



Additional Info:
- When I run the code above, it generates error that says Run-time error 91: Object variable or With block variable not set

- I'm using ADO control to connect my vb6 form to access.

I have been going around in circle for quite some time now, but it seems I can't find the exact way to solve it.

I'd really appreciate any help I could get.

Thanks a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing vb6 option button value to an access field

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing vb6 option button value to an access field

Posted 23 October 2012 - 02:25 PM

Does every line of code generate that error?
Was This Post Helpful? 0
  • +
  • -

#3 madujr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 12

Re: Passing vb6 option button value to an access field

Posted 23 October 2012 - 04:11 PM

The line after the IF statement generates error.
myRS.Fields!Gender = "Male"
and
myRS.Fields!Gender = "Female"
Was This Post Helpful? 0
  • +
  • -

#4 madujr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 12

Re: Passing vb6 option button value to an access field

Posted 23 October 2012 - 08:40 PM

Maybe I need statement/s that would connect my Option buttons to the database, cause it's not bind to the ADO control. And perhaps some initializations.

It's what I learned so far from readings several forums.
Can anybody help me on this?
Any idea is welcome.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing vb6 option button value to an access field

Posted 24 October 2012 - 07:14 AM

It might have been helpful to tell which line was causing the error in the first place. :) Perhaps in future you will do so when putting up posts?

Ok. There's more to opening up a recordset than just declaring a recordset variable. You have to establish a connection, and then open the recordset using that connection. I'm not going to tell you exactly how to do it because you'll learn better if you attempt to do it yourself. So, read up on ADO connections and ADO recordsets, paying attention to the Recordset's Open method and the Connection's ConnectionString property. If you need help with the latter, look at www.connectionstrings.com.
Was This Post Helpful? 0
  • +
  • -

#6 madujr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 12

Re: Passing vb6 option button value to an access field

Posted 24 October 2012 - 06:04 PM

Yes bob, you're right. stating the error line should be helpful. This is my first post, so that should be a good point to learn. I appreciate that as well as your time with this post.

Anyway, establishing the ADO connection is really my problem. I'll go and check your reference to try to learn it. I'll be back here if I solve the problem.

Thanks bob.
Was This Post Helpful? 0
  • +
  • -

#7 madujr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-July 12

Re: Passing vb6 option button value to an access field

Posted 24 October 2012 - 08:16 PM

Hello everyone.

I think I found a simpler way to send option button value to an access field. It's through the use of a label as handler of the option button's value which is in "true" state(clicked option button). Then the label control will issue that value it received from the option button to the database with the use of of Data or ADO control.

Here's the sample code:
Private Sub Command1_Click()
    If Option1.Value = True Then
        genderLbl = "Male"
    ElseIf Option2.Value = True Then
        genderLbl = "Female"
    End If
    
Data1.Recordset.Update
    
End Sub

Private Sub Command2_Click()
    Data1.Recordset.Edit
End Sub



With this code, you no longer need to worry about building a connection between the option buttons and the database. I suspect though that this may tend to be a little slower since your option button does not communicate directly to the database. But it works just fine.

Hope this is helpful to beginners like me.
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing vb6 option button value to an access field

Posted 25 October 2012 - 02:29 PM

Good job getting your program running. :) Now, the Data Control is ok, but not always easy to work with as you get more advanced. Also, data binding as you are describing here is problematical in VB6. However, if it works fine, then there you are. First time that you have trouble with this approach consider abandoning it for the ADO objects.

By the way, I feel safe surmising that the reason that you're using labels is because you couldn't figure out how to bind the option buttons directly. I therefore assume that you haven't put them in a frame. Try putting them in a frame (make sure that if you move the frame, the buttons move too) and binding the frame to the ControlSource property. If it works, you can bung your labels and your bit of code.

This post has been edited by BobRodes: 25 October 2012 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1