how do i set the database ?

like this i has 1 data control in my form and 3 text box.

i want to connect the data control to abc.mdb
and inside the abc.mdb therea 2 table
and i want to connect that data control to abc.mdb withe the table no 2 and also inside that table i has 3 field.
so i want my 3 text box to become that 3 field.

i know i can do it in the properties windows.

but now i want to do it manually in the code .

how ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Private Sub Form_Load()
    With Data1
        .DatabaseName = App.Path & "\abd.mdb"
        .RecordSource = "SELECT * FROM TheTable;"
    End With
End Sub

Private Sub Data1_Validate(Action As Integer, Save As Integer)
    Text1 = Data1.Recordset.Fields(0)
    Text2 = Data1.Recordset.Fields(1)
    Text3 = Data1.Recordset.Fields(2)
End Sub

JOHNHOAuthor Commented:

Try it like this....

Private Sub Form_Load()
   With Data1
      .DatabaseName = YourMDB  ' This can be App.Path & "\your.mdb"
      .RecordSource = "YourTable" ' or query
   End With
End Sub

Now, you should really attach the text boxes to the database so the user can alter the data (or you can lock the text boxes so they can't. If you want to READ data in  and let them edit it but not save back to the database, then reading the data through code is a good idea. But if you want the user to be able to update and save back to the database, you should connect the text boxes to the database.

You can set the connection to the data control at DESIGN time only unless you are using ADO then you can do it at run time.

----- From MSDN -------------------

Use the Set statement to set the DataSource property, as shown below:

Set Text1.DataSource = ADODC1

Note   Two older controls, the Data control and RemoteData control, can be used as data sources, however you cannot set the DataSource property of another control or object to either of these controls at run time. For example, the following code will fail:

Set Text1.DataSource = Data1    ' Will fail! You can't set DataSource at
                              ' run time to an intrinsic Data control.


You can also attach the textbox controls DataFields at design time. Or you can do it at run time at the start of the Form_Load routine BEFORE connecting to the database.

Text1.DataField = "somefield"
To use either the Data control or RemoteData control as a data source, you can set the DataSource property of bound controls at design time only.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

You should accept (or reject) someone's answer or comment - thanks is not enough   ;-)

same as above

except that
after connecting to database

set text3.datasource = MyRecordSet


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.