Two records being added to table MS Access - vba

I have a form which allows the user add a record to a table but when the Create Operation button is clicked two records are added to the table instead of one. When I add a second the extra record is then changed to the newest record added. This continues to happen and there is always 1 extra record in my table. How would I go about changing this ?
Here is the code I am using to add the record :
Private Sub Save_Operation_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
strElement = Me.Element.Value
strOperation = Me.Operation.Value
strProduct = Me.Product_ID.Value
strTime = Me.Time.Value
strQty = Me.Qty.Value
Set db = CurrentDb
Set rs = db.OpenRecordset("Labour", dbOpenTable)
rs.AddNew
rs("Element").Value = strElement
rs("Operation").Value = strOperation
rs("Product_ID").Value = strProduct
rs("Time").Value = strTime
rs("Qty").Value = strQty
rs.Update

i think the problem is the use of "addnew" so try this :
With rs
.AddNew
!Element = strElement
!Operation = strOperation
!Product_ID = strProduct
!Time = strTime
!Qty = strQty
.Update
End With

I came across this same problem and after a lot of confusion, I figured out the issue for me. I had set the Control Source on the fields on the form to the table I was adding records to with the button. I had to remove the Control Source completely, and then the double entries stopped.

Related

MS Access: How to load data from query into form

I have a form that helps the user add some data into a Database, however I noticed that a few of the fields on each product added are very similar. So I'm adding a second database of commonly used fields that can be pre-filled by selecting from a combo-box.
For example if the user is adding Product XX-X but product XX-X is of the same family of YY-Y and I already have YY-Y's data in Database2, I want to just load those parameters. I made a query that returns the parameters as I want however I dont know how to add this to the FORM.
Basically I have a blank VBA code slot for "ComboBox_Change".
I want the ComboBox_Change function to load field X from query and paste it into field X1 on the current form.
Hope I'm explaining myself correctly.
Thanks!
THANKS for the suggestion this is the code so far that has an error
Private Sub LoadMatCB_Change()
Dim rs As Recordset
Dim db As Database
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset("VendorDeetsQuery")
If Nz(Me.Input_Vendor.Value, "") = "" Then Me.Input_Vendor.Value = rs![Origin]
Set rs = Nothing
Set db = Nothing
End Sub
You will need to use the table's ID field to grab the related record from your query into a recordset object. You can then use the fields of the recordset to set the value in each of the other fields you want to populate; you can add the if statements to check if the controls are blank if that is a condition you need.
Dim rs As Recordset
Dim db as Database
Dim qry as QueryDef
Set db = CurrentDb
Set qry = db.QueryDefs("YourQueryName")
qry.Parameters("ParamName") = comboBox.Value '<pass your parameter here>
'repeat the above for any other parameters you need to pass
Set rs = qry.OpenRecordset
'for each of these, use your control names and whatever you named the fields from your query'
If Nz(Me.txtBox1.Value, "") = "" Then Me.txtBox1 = rs![fieldName1]
If Nz(Me.txtBox2.Value, "") = "" Then Me.txtBox2 = rs![fieldName2]
...
...
If Nz(Me.txtBoxN.Value, "") = "" Then Me.txtBoxN = rs![fieldNameN]
Set rs = Nothing
Set qry = Nothing
Set db = Nothing

DAO recordset filter function cannot filter with 2 properties

I am using VBA in MS Access 2010.
I am currently trying to filter from a recordset with 2 fields.
However i tired, it will not filter as per what i want.
But if i were to filter based on only one field, the recordset is able to filter accordingly.
This is what I have now.
Private Function getCheckedRecordsFromDB(ByVal cmNum As String) As Boolean
Dim rs As Recordset
Dim rsFiltered As Recordset
Dim iSeral As Integer
'Gets different fields from different tables and store them into rs
Set rs = CurrentDb.OpenRecordset("QueryMemoOutFrm")
' Its not working during the filtering, keeps returning nothing found
rs.Filter = "Doctype='Outgoing' AND DocumentRef='" & cmNum & "'"
Set rsFiltered = rs.OpenRecordset
Do While Not rsFiltered.EOF
' Do Something
Loop
rs.Close
Set rs = Nothing
rsFiltered.Close
Set rsFiltered = Nothing
End Function
I have read the documentation on MSDN, and does not see where did i go wrong. (Maybe i've missed out something)
I have changed the codes a little bit and it works. But not sure why though.
Private Function getCheckedRecordsFromDB(ByVal cmNum As String) As Boolean
Dim rs As Recordset
Dim rsFiltered As Recordset
Dim dSerial As Double
'Gets different fields from different tables and store them into rs
Set rs = CurrentDb.OpenRecordset("QueryMemoOutFrm")
rs.Filter = "Doctype='Outgoing' AND DocumentRef='" & cmNum & "'"
Set rsFiltered = rs.OpenRecordset
' newly added
rsFiltered.MoveFirst
Do While Not rsFiltered.EOF
dSerial = rsFiltered!SerialNo
rsFiltered.MoveNext
Loop
rs.Close
Set rs = Nothing
rsFiltered.Close
Set rsFiltered = Nothing
End Function
It's much easier to troubleshoot if your provide your actual code via copy/paste.
That being said, I'm just wondering why you're using two recordsets?
Do you get the proper answer if you actually do your loop on the real filtered recordset?
Eliminate the line
Set rsFiltered = rs.OpenRecordset
Use this block of code on rs instead of rsFiltered
Do While Not rs.EOF
' Do Something
Loop

Access VBA: Put values of a multi-select list box into a Table

I have a list box where I can select Monday- Friday. I can select as many days as I want in the list box, all of them if I wanted. I want to know how to insert the value of the listbox into my table.
Here's the code I've written so far:
Private Sub Command499_Click()
Set RstRecSet = Nothing
Set db = CurrentDb
Dim dateDay As String
Dim dateWeek As String
MsgBox (lstDateDay.Selected)
''dateWeek = lstDateWeek.Value
db.Execute " INSERT INTO tblContacts (DateDay, DateWeek)Values" & "('" & dateDay & "', '" & dateWeek & "');"
db.Close
End Sub
As you can see I've been trying a lot of different things. My problem is getting the value of the list box; it keeps showing as null even though it has data selected. The exact error I'm getting is:
"Invalid use of Null."
EDIT:
Set rs = db.OpenRecordset("tblContacts")
For Each itm In lstDateWeek.ItemsSelected
rs.AddNew
rs!dateWeek = lstDateWeek.ItemData(itm)
rs!dateDay = itm
rs.Update
Next
rs.Close
Set rs = Nothing
Set db = Nothing
dateDay and dateWeek are columns in tblContacts.
You need to use the ItemsSelected collection to get the index of the selected items in the Multi Select list box and then iterate through them and use the index to reference the rows stored in the ItemData collection. As part of this iteration simply create a record set and add the fields and update. There are different ways to handle this part but I like this one shown below.
To use my sample, simply create a table called tblTest and two columns Description (text) and Day as a number.
Create a form and add a multi-select list box named DaysOfWeek. fill it in with the days of the week as a ValueList and then add a button which I labeled Store.
Paste the following code into the buttons click event and try it
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblTest")
For Each itm In DaysOfWeek.ItemsSelected
rs.AddNew
rs!Description = DaysOfWeek.ItemData(itm)
rs!Day = itm
rs.Update
Next
rs.Close
Set rs = Nothing
Set db = Nothing
My event procedure looks like this:
Private Sub Command19_Click()
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblTest")
For Each itm In DaysOfWeek.ItemsSelected
rs.AddNew
rs!Description = DaysOfWeek.ItemData(itm)
rs!Day = itm
rs.Update
Next
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
This could be done with an ADO call by building a SELECT string for your INSERT statement as well but for me this is straightforward.. If you have any questions let me know. If I can figure out how to attach my sample database I will.

MS Access 2010 - Instantiate a child recordset using DAO and get "No current record" error

I'm trying to move data from a table1 in database 1 to table2 in database 2. Table 1 has the same fields as table 2. Table 1 has data and Table 2 don't have.
rsDenuncia is the recordset of table 1 from database 1
regNuevoDenuncia is the recorset of table 2 from database 2
I instantiate a child recordset of rsDenuncia that is named rsODenuncia and it works because
I can retrieve data using msgboxes.
Set rsODenuncia = rsDenuncia.Fields("pdfAdjunto").Value
MsgBox "Nombre el archivo: " & rsODenuncia("FileName").Value
MsgBox "Tipo de archivo: " & rsODenuncia.Fields("FileType").Value
MsgBox "Data del archivo: " & rsODenuncia.Fields("FileData").Value
But when I try to instantiate the recordset of table 2 (that is an empty table but with same fields as table 1) is gives a error. "No current record"
Set regONuevoDenuncia = regNuevoDenuncia.Fields("pdfAdjunto").Value
Is there a way to instantiate regONuevoDenuncia without using the Value method so I can add new data?
Thanks in advance.
You need to create the new parent record with regNuevoDenuncia.AddNew before you can open the child recordset for its attachments. The following code is a minimal example:
Option Compare Database
Option Explicit
Sub CopyRecordsWithAttachments()
Dim cdb As DAO.Database
Dim rsDenuncia As DAO.Recordset2, rsODenuncia As DAO.Recordset2
Dim regNuevoDenuncia As DAO.Recordset2, regONuevoDenuncia As DAO.Recordset2
Set cdb = CurrentDb
cdb.Execute "DELETE FROM NuevoDenuncia", dbFailOnError ' clear previous test data, if any
Set rsDenuncia = cdb.OpenRecordset("Denuncia", dbOpenSnapshot)
Set regNuevoDenuncia = cdb.OpenRecordset("NuevoDenuncia", dbOpenDynaset)
Do Until rsDenuncia.EOF
regNuevoDenuncia.AddNew
regNuevoDenuncia!ID = rsDenuncia!ID
Set rsODenuncia = rsDenuncia.Fields("pdfAdjunto").Value
Set regONuevoDenuncia = regNuevoDenuncia.Fields("pdfAdjunto").Value
Do Until rsODenuncia.EOF
regONuevoDenuncia.AddNew
regONuevoDenuncia!FileName = rsODenuncia!FileName
regONuevoDenuncia!FileData = rsODenuncia!FileData
regONuevoDenuncia.Update
rsODenuncia.MoveNext
Loop
regONuevoDenuncia.Close
Set regONuevoDenuncia = Nothing
rsODenuncia.Close
Set rsODenuncia = Nothing
regNuevoDenuncia.Update
rsDenuncia.MoveNext
Loop
regNuevoDenuncia.Close
Set regNuevoDenuncia = Nothing
rsDenuncia.Close
Set rsDenuncia = Nothing
Set cdb = Nothing
Debug.Print "Terminado."
End Sub
You can't reference that field, in the manner you are, in the recordset for DB2 until the recordset is populated. Once you add records to DB2, then the error will go away.
Without seeing all your code, I suggest you do something like:
If not regNuevoDenuncia.eof then
Set regONuevoDenuncia = regNuevoDenuncia.Fields("pdfAdjunto").Value

How can I insert data into Access database fields?

I have created multiple forms and each contains multiple buttons. The end-user will go through the forms sequentially where he selects one button and goes to the next form and so on.
I also created a table that stores the entered data. For example; if the user clicks Button A, the designated field will have value of "A" and so on. In other words, each button will enter a certain data into a specific field.
What I wanted eventually is to have a complete record set (row) after the set of forms finishes.
I used the following code inside the very first button in the first form
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Data", dbOpenDynaset)
RS.AddNew
RS("WLAN") = "ARUBA"
RS.Update
Set RS = Nothing
In the later forms (buttons), I used the following code
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Data", dbOpenDynaset)
RS.Edit
RS("Security") = "WPA2-PSK"
RS.Update
Set RS = Nothing
Now what happens is that the first sequence of forms (buttons) successfully enter data into the first row in my table [Data] but when I start over and select other buttons, the existing row get edited with the new selections.
What I want is to add a new record and insert data into it and not to edit the previous one.
I will highly appreciate your kind help and i'm sorry for the long post.
Thank you,
Nasser
Create a variable to hold the id of the record you are working on. I will call it RecId for this example.
To add a record
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Data", dbOpenDynaset)
RS.AddNew
RS("WLAN") = "ARUBA"
RecId = RS("ID")
RS.Update
Set RS = Nothing
Then to Update the same record
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Data", dbOpenDynaset)
RS.MoveFirst
Do Until RS.EOF
If RD!ID= RecId Then
RS.Edit
RS("Security") = "WPA2-PSK"
RS.Update
End If
RS.MoveNext
Loop
Set RS = Nothing
If you never want to update any records and only ever want to add new then only use the add code. You will need to put a few check in but this should give you the basis of what you need.