[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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 ?

1 Solution
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.

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



Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now