S
SA
Version: Access 2007
Hi
I am new to access and vba. The solution to the problem that I have should
be quite easy for you guys but to me, still unknown. However, I want to add
some code to a button on a form to add single record to the corresponding
table using .AddNew method. For the sake of simplicity I am working on a
single table (only two fields: ID and Qty) and a form and the code is as
follow:
Private Sub cmdSave_Click()
Dim rs As ADODB.Recordset
On Error GoTo HandleError
Set rs = New ADODB.Recordset
rs.Open “tempTableâ€, CurrentProject.Connection, _
adOpenDynamic, adLockOptimistic
With rs
.AddNew
![Qty] = Qty
.Update
End With
rs.Close
Set rs = Nothing
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
End Sub
Now the problem is:
1.When I enter value of Qty on the form and click the button to save it on
the table, it creates duplicate values in a strange way. After clicking the
button, if I open the table to see if data is posted keeping the form open,
it shows single record as it should be. But when I close the form and refresh
the table clicking refresh button or reopen the table it duplicates the value.
2.If I hard code the value of Qty like Qty = “100â€, the code works just
fine. But when you replace the value with ![Qty] = Qty, then is the problem.
3.For testing this further, I added a line Qty = Ҡafter .Update to see
what happens. In this case, I found the same duplicate effect but first
record is blank, only Id has the autonumber value.
How can I use the .AddNew method properly so that I can get the desired
result?
Another question, if I want to use .bookmark on record, how can I use it? If
I use it like .BookMark = .LastModified after .upadate, it generates an error
message saying “Method or Data Member not foundâ€.
I’ll appreciate, if anyone helps me on this.
Thanks.
Hi
I am new to access and vba. The solution to the problem that I have should
be quite easy for you guys but to me, still unknown. However, I want to add
some code to a button on a form to add single record to the corresponding
table using .AddNew method. For the sake of simplicity I am working on a
single table (only two fields: ID and Qty) and a form and the code is as
follow:
Private Sub cmdSave_Click()
Dim rs As ADODB.Recordset
On Error GoTo HandleError
Set rs = New ADODB.Recordset
rs.Open “tempTableâ€, CurrentProject.Connection, _
adOpenDynamic, adLockOptimistic
With rs
.AddNew
![Qty] = Qty
.Update
End With
rs.Close
Set rs = Nothing
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
End Sub
Now the problem is:
1.When I enter value of Qty on the form and click the button to save it on
the table, it creates duplicate values in a strange way. After clicking the
button, if I open the table to see if data is posted keeping the form open,
it shows single record as it should be. But when I close the form and refresh
the table clicking refresh button or reopen the table it duplicates the value.
2.If I hard code the value of Qty like Qty = “100â€, the code works just
fine. But when you replace the value with ![Qty] = Qty, then is the problem.
3.For testing this further, I added a line Qty = Ҡafter .Update to see
what happens. In this case, I found the same duplicate effect but first
record is blank, only Id has the autonumber value.
How can I use the .AddNew method properly so that I can get the desired
result?
Another question, if I want to use .bookmark on record, how can I use it? If
I use it like .BookMark = .LastModified after .upadate, it generates an error
message saying “Method or Data Member not foundâ€.
I’ll appreciate, if anyone helps me on this.
Thanks.