How can I insert data into Access database fields? - sql

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.

Related

Access Continuous form based on recordest made with VBA (fill the form fields)

I've prepared recordset and open form with this.
The form works with recordset - quantities of records in the form is the same like on the recordset.
Problem:
I have absolutely no idea how to display the recordset results in the form fields.
I know one way - create an assistant table but I would like to avoid it.
Any idea please ?
Dim rst As Object
Set rst = CreateObject("ADODB.Recordset")
....
With rst
.Fields.Append "date", 7
.Fields.Append "index", 3
End With
rst.Open
......
Set Forms!Form1.Recordset = rst
Thank you for your interesting,
I have to create recordset.
I need to make some calculation between two table and only way I find it is with vba (no way with sql).
Anyway it shoud be a way to open form with recordset.
You cane create a simple continuous form ("Form1") with one blank field.
The code below is for loading form.
As you can see you'll open the form with 4 blank field.
How to put code result (0,1,2,3) into form fields ?
Private Sub Form_Load()
Dim rst As Object
Dim i As Integer
Set rst = CreateObject("ADODB.Recordset")
rst.Fields.Append "Field_1", 3
rst.Open
For i = 0 To 3
With rst
.AddNew
![Field_1] = i
.Update
End With
Debug.Print i
Next
Set Forms!Form1.Recordset = rst
Set rst = Nothing
End Sub

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

In a continuous form, can I have a combo box use a different query depending on a field or text box value within its own record?

Edit: for those wondering, it's apparently impossible to have the same combo box for two different records in a continuous form refer to two different queries to populate its list.
I have a continuous form that has maybe 5 records. There is a combo box, Laborer1, and would like the dropdown to be different for each form, depending on some other factors. I managed to put the exact query I want for each record's combo box as a text field within that record. What is the next step? All I can manage at this point is apply the query of one record to all combo boxes, but I want each combo box to use its "own" query.
Thanks!
Yes, you can do it, but there's a tradeoff: inactive records may have a value which doesn't fit within the current rowsource for the combobox. When that happens, you'll get a blank combobox, instead of having it show the current value. If you activate the record, the value will appear again, but it's not a fantastic user experience.
That said, one option would be to handle it in the Form_Current event. Since you're already storing the rowsource in a database field, the code for this is really short:
Private Sub Form_Current
Laborer1.RowSource = ReferenceField.Value
Laborer1.Requery 'I don't believe you need this, but you might.
End Sub
I think I have an answer for continuous forms. When u click on combobox other combos turns blank but when u select everything looks normal.
I worked with my tables so sorry for a different example.. Anyway I think It is an answer..
I have 2 combobox and when u select the country the city combo shows only the selected Country's Cities.
This code is for Country Combo
Private Sub Country_Change()
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Cities", dbOpenTable)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("Select * FROM Cities WHERE Country='" + Country + "'", dbOpenDynaset)
'We created a recordset filtered and set it to city.recordset
Set city.Recordset = rsQuery
city.Requery
End Sub
And this 2 sub is for City Combo..
Private Sub City_Change()
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Cities", dbOpenTable)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("Select * FROM Cities", dbOpenDynaset)
Set city.Recordset = rsQuery
'We created a recordset NOT filtered and set it to city.recordset and after that we focus on a different control to be sure that our city combobox work correctly
city.Requery
Kimlik.SetFocus
End Sub
Private Sub City_Click()
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Cities", dbOpenTable)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("Select * FROM Cities WHERE Country='" + Country + "'", dbOpenDynaset)
'We created a recordset filtered and set it to city.recordset
Set city.Recordset = rsQuery
city.Requery
End Sub
And here is my Access file

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.

Two records being added to table MS Access

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.