Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Adding data to table using forms.

I am three fields in "Table1". I created a form with three textbox exactly like the ones used as field heading in "Table" 1.I also added a button that is supposed to add records to "Table1" when clicked. I am very new to access coding. Someone please help me write code to this button so that I can populate the table using form

Table1

Field1   Field2   Field3(combo with valued 1,2,3)


Form1

Field1 ________
Field2________
Field3________            AddButton.


Thank you.
0
Aiysha
Asked:
Aiysha
  • 2
1 Solution
 
vinnyd79Commented:
Here is a simple example in VB. Maybe it can help:


' add a reference to Microsoft ActiveX Data Objects Library:
Dim DataBase As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub Form_Load()
DataBase = "C:\MyDataBase.mdb"

' open Database Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & DataBase & "'"
cn.CursorLocation = adUseClient
cn.Open

' open recordset
Set rs = New ADODB.Recordset
' EDIT TABLENAME BELOW
rs.Open "Select * From YourDBTableName", cn, adOpenKeyset, adLockOptimistic

End Sub


Private Sub Command1_Click()
' add new record to Table
rs.AddNew
rs.Fields("Field1").Value = Field1.Text
rs.Fields("Field2").Value = Field2.Text
rs.Fields("Field3").Value = Field3.Text
rs.Update

End Sub


Private Sub Form_Unload(Cancel As Integer)
' close recordset
rs.Close
Set rs = Nothing

' close connection
cn.Close
Set cn = Nothing
End Sub

0
 
vinnyd79Commented:
Just replace YourDBTableName with Table1 and it should work.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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