Access VBA Moving from one listbox to another - ms-access

I have a listbox named lstAvailable and i am moving the item selcted from this to another listbox named lstAssigned and also the value that gets sent here is stored in the table tblComponents. The values are storing in the table but i am not able to see the moved item in the listbox lstAssigned. Can someone help me?
The Code is
nRid = listMachineSubSystem.Column(1)
For Each varRow In lstAvailable.ItemsSelected
nEid = lstAvailable.Column(0, varRow)
sSQL = "INSERT INTO tblComponents ([Machine Subsystem ID], [NewComponents])" _
& " VALUES (" & nRid & ", " & nEid & ");"
CurrentDb.Execute sSQL, dbFailOnError
Next varRow
lstAvailable.Requery
lstAssigned.Requery
For Each varItem In lstAvailable.ItemsSelected
lstAvailable.Selected(varItem) = False
Next
For Each varItem In lstAssigned.ItemsSelected
lstAssigned.Selected(varItem) = False
Next
End Sub

Related

On updating shows message: Save or drop changes or copy to clipboard

I was asked to capture the date when a specific field is updated , so I created an event to update the record of that field.
Dim db As Database
Dim strSQL As String
Dim LDate As String
LDate = Format(Date, "yyyy-mm-dd")
Set db = CurrentDb
strSQL = "UPDATE [Lotinfo] " & _
"SET [PriorityChanged] = " & _
Chr(34) & LDate & Chr(34) & _
" where [BKPO#] = " & _
Chr(34) & Forms![LotTabFrm]![LotInfoPriority]![BKPO#] & Chr(34) & _
" and [ModelNo] = " & _
Chr(34) & Forms![LotTabFrm]![LotInfoPriority]![ModelNo] & Chr(34)
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
The update does happen, however it keeps showing a 'copy to clipboard' message box asking me to drop the changes or copy to clipboard and in both cases the changes are lost
Is there a way to stop that message box from showing up?
You are getting this message because Ms Access has a conflict. Should it save the values you entered into the form or rather the values you are entering now using the UPDATE statement?
Assuming that you want the values from the UPDATE statement, add the following line before the UPDATE to save the Form values first:
DoCmd.RunCommand acCmdSaveRecord

Assign control source dynamically from a different recordset

I have the following VBA code in my access.
Dim subform As Object
Dim formFilter As String
formFilter = "..." 'a SQL statement
Set subform = Me!my_subform.Form
subform.RecordSource = formFilter
subform.field1.ControlSource = "f1"
subform.field2.ControlSource = "f2"
subform.field3.ControlSource = "f3"
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT TOP 1 f4 FROM my_table " _
& "WHERE tableF1= '" & [f1] & "' AND tableF2 = '" & [f2] & "' " _
& "ORDER BY tableF5 DESC")
subform.field4.ControlSource = rs(0)
I have first bound my first 3 fields in subform to the fields of my record source. Then I need to bind the 4th field to a different recordset. This recordset has to refer to the first 2 fields of my subform.
However, I got a run-time error 2465. Access is not able to refer to the field [f1] and [f2] of my OpenRecordSet statement.
How should I fix this?
I use this form in a datasheet view. So I need to refer to not a single value of field1 and field2, but the entire columns of records have to be linked.
Thanks a lot.
(from an earlier edit to the question, since rolled back:)
Apparently the solution in this case was to use the following code in the On Load event handler for the subform instead of the main form
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT TOP 1 f4 FROM my_table " _
& "WHERE tableF1= '" & [f1] & "' AND tableF2 = '" & [f2] & "' " _
& "ORDER BY tableF5 DESC")
subform.field4.ControlSource = rs(0)
because the [f1] and [f2] controls were on the subform and therefore not visible from the Class Module code for the main form.

Getting the value from the first row where column name is <value>

I have a form where a user inserts some values
When the user clicks the "ok" button Access makes a new table to save the values from disappearing when the form closes.
strSQL = "CREATE TABLE tblTempProjectGegevens (Project varchar(32),ProjectNummer varchar(32), Opdrachtgever varchar(32));"
DoCmd.RunSQL (strSQL)
strSQL = "INSERT INTO tblTempProjectGegevens (Project, ProjectNummer, Opdrachtgever)" & _
"VALUES ('" & ProjectInvoer.Value & "', '" & ProjectNrInvoer.Value & "', '" & OpdrachtgeverInvoer.Value & "');"
DoCmd.RunSQL (strSQL)
DoCmd.OpenForm "frmMain", acNormal, , , acFormAdd
DoCmd.Close acForm, "frmProjectInvoer", acSaveNo
I save these values in a table, because my program requires the form to be closed.
When the user is done using my access file, there are some reports to be printed. On these reports are headers in wich I want the values I have saved in the temp table.
How do I get the values from the table in my report headers? During a report_load() event?
Private Sub Report_Open(Cancel as Integer)
Dim db as Database
Dim rec as Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("tblTempProjectGegevens")
Me.Project.Value = rec("Project")
Etc...
End Sub

how to update a mainform into a subform

After I edit information and change the information and click update, it gives me a error. I tried the parenthesis brackets no luck.
Too few parameters Expected 1. Run time error '3061'
Private Sub cmdUpdate_Click()
Dim strSql As String
strSql = "UPDATE PlantTransaction " & _
"SET TransactionID=" & Me.txtTranID & _
",[Plant Number]='" & Me.txtPlantNo & "'" & _
",TransactionDate=#" & Me.txtTransDate & "#" & _
",Opening_Hours='" & Me.txtOpeningHRS & "'" & _
",Closing_Hours='" & Me.CloseHrs & "'" & _
",Fuel='" & Me.txtFuel & "'" & _
",[Fuel Cons Fuel/Hours]='" & Me.txtFuelConsFuelHr & "'" & _
",[Hour Meter Replaced]='" & Me.txtHrMtrRep & "'" & _
",Comments='" & Me.txtComments & "'" & _
",[Take on Hour]='" & Me.txtTOH & "'" & _
" WHERE TransactionID=" & Me.PlantTransactionQuery.Form.Recordset.Fields("Tr ansactionID")
Debug.Print strSql ' <- prints to Immediate window
CurrentDb.Execute strSql, dbFailOnError
cmdClear_Click
Me.PlantTransactionQuery.Form.Requery
End Sub
You were smart to include this line in your code:
Debug.Print strSql ' <- prints to Immediate window
Now when you get the missing parameter message, go to the Immediate window (you can use Ctrl+g to go there) and copy the SQL statement.
Then create a new Access query in the query designer, switch to SQL View, and paste in the text you copied. When you attempt to run that query, Access will present a parameter input box which includes the name of whatever it thinks is the parameter.
Compare that parameter name with the field names in your data source. Often this situation occurs because the query includes a misspelled field name. Another possibility with an UPDATE is that one of the values you're trying to update is unquoted text. Regardless of the cause, the parameter name from that input box should help you track it down. Show us the actual text from that UPDATE statement if you need further help.
Any time that you "glue together" a long SQL statement with lots of user input you face the challenges of
correctly delimiting strings and dates,
escaping delimiters within such fields (usually quotes inside a text field), and
getting all of the required commas in the right places
You can avoid those annoyances by using a Recordset to perform the update:
Dim rst As DAO.RecordSet
Set rst = CurrentDb.OpenRecordset("PlantTransaction", dbOpenDynaset)
rst.FindFirst "TransactionID=" & Me.PlantTransactionQuery.Form.Recordset.Fields("Tr ansactionID")
If Not rst.NoMatch Then
rst.Edit
rst!TransactionID = Me.txtTranID
rst![Plant Number] = Me.txtPlantNo
rst!TransactionDate = Me.txtTransDate
rst!Opening_Hours = Me.txtOpeningHRS
rst!Closing_Hours = Me.CloseHrs
rst!Fuel = Me.txtFuel
rst![Fuel Cons Fuel/Hours] = Me.txtFuelConsFuelHr
rst![Hour Meter Replaced] = Me.txtHrMtrRep
rst!Comments = Me.txtComments
rst![Take on Hour] = Me.txtTOH
rst.Update
End If
rst.Close
Set rst = Nothing

MS-Access: Using ListBox selections from one form to filter results in another form

Here is my problem:
I am working with MS-Access 2010. I have a form with a listbox reserved for employee names and employee IDs with 2 command buttons below. One button displays all of my employees. The other opens a modal form with filter criteria. In this model form i have another listbox with different HR categories (with multi-select enabled).
What I am attempting to do is: open my modal form, select one or more of these HR categories in my listbox, click a command button that then closes the modal form and updates the employee listbox based on the certain criteria i have selected.
Thanks in advance for help with this. This is driving me a little nuts :-)
Here is my updated code below.
Private Sub cmd_ExecuteEmpFilter_Click()
Dim varItem As String
Dim i As Variant
Dim strSql As String
'Building the criteria string from selected items in the list box
varItem = ""
For Each i In Me!lst_HRFilter.ItemsSelected
If varItem <> "" Then
varItem = varItem & " , "
End If
varItem = varItem & Me!lst_HRFilter.ItemData(i) & ""
Next i
'Filter the form using selected items inside the list box
txt_HRCat = varItem
strSql = "SELECT DISTINCT q.LastName, q.FirstName, q.EmployeeID " & _
"FROM qry_MasterEmployeeFilter As q " & _
"WHERE q.HomeTeamID=" & Me.txt_TeamID2 & _
"AND q.ReportingGroupID IN (" & varItem & ") " & _
"ORDER BY q.[LastName], q.[FirstName], q.[EmployeeID];"
Debug.Print strSql
Forms!frm_Employee_Updater.lst_AllEmps.RowSource = strSql
DoCmd.Close acForm, "frm_EmployeeFilter", acSaveYes
End Sub
you need to requery the main form after you have changed its recordsource.
Forms!frm_Employee_Updater.lst_AllEmps.Requery