1
votes

Just a headsup, I am quite new to relational databases so my question could be a simple fix.

Currently, I have a table with the following data.

ID(1 ,2,3,4,5)

Location(Canada, USA, Japan, Australia, Venezuela)

Count(4,6,2,91,23)

I created a form with a combobox and two text fields. The goal is that I want to be able to click the combobox, have it show all the ID's and when I click an ID, it autopopulates the other two text fields with the corresponding information. After googling a bit, I found a way to do is. Within the Event tab under "on Change" for the combobox, I wrote the these two lines of code.

Me.txtLocation = Me.cboID.Column(1)

Me.txtCount = Me.cboID.Column(2)

However, I also want to be able to edit this information once it has been autopopulated. The problem I'm having is that when I change any of the two textfields, it always edits the first records.

So for example, if I click ID #4, and I change the "Count", it will change the "Count" for the ID #1. Any idea of what I'm doing wrong?

P.S. (I have programming experience but not with VBA)

Thanks in advance!

EDIT:

Private Sub txtCount_AfterUpdate()

Dim strSQL As String

strSQL = "UPDATE aDuh SET Count = '" & Me.Count & "', Location = '" & Me.txtLocation & "' WHERE ID = " & Me.cboID & ""

DoCmd.RunSQL (strSQL)

End Sub

Private Sub txtLocation_AfterUpdate()

Dim strSQL As String

strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = " & Me.cboID & ""

DoCmd.RunSQL (strSQL)

End Sub

1

1 Answers

2
votes

Don't bind your form to a table. Put a textbox on your form with it's Visible property set to False. In that textbox, put the value of your Primary Key field (which should be an AutoNumber field) Then when you update your record, pass an UPDATE SQL statement where you update your table based on the value of your Primary Key

Dim strSQL as String

strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = " & Me.cboID & ""

DoCmd.RunSQL (strSQL)