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
Related
I'm trying to generate a report similar to a crosstab. The data are from a filtered form (Dates and WorkerID (String)).
form: frmReg
table with data: tReg
report: reportReg
On the following line:
Set qdf = dbsReport.QueryDefs(Me.RecordSource)
I'm getting the error:
Error 3265 Item not found in this collection
What am I doing wrong?
Private Sub Report_Open(Cancel As Integer)
' Create underlying recordset for report using criteria
Dim intX As Integer
Dim qdf As QueryDef
Dim frm As Form
' Don't open report if frmReg form isn't loaded.
If Not (IsLoaded("frmReg")) Then
Cancel = True
MsgBox "To preview or print this report, you must open " _
& "frmReg in Form view.", vbExclamation, _
"Must Open Dialog Box"
Exit Sub
End If
' Set database variable to current database.
Set dbsReport = CurrentDb
Set frm = Forms!frmReg
' Open QueryDef object.
' Set qdf = dbsReport.QueryDefs("ReportReg")
Me.RecordSource = "SELECT * FROM [tReg]"
Set qdf = dbsReport.QueryDefs(Me.RecordSource)
' Open Recordset object.
Set rstReport = qdf.OpenRecordset()
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
End Sub
It looks like the problem might be a relationship issue between the SQL and the commands and you probably do not have a query setup to take the information you are seeking.
Try this:
sSQL = "SELECT * FROM [tReg]"
Me.RecordSource = sSQL
Set qdf = dbsReport.CreateQueryDef("NewQuery", sSQL)
'This will purge the query after your inteactions are complete
dbsReport.QueryDefs.Delete "NewQuery"
Note: This will not include any interactions for the QueryDef.
The QueryDefs collection takes saved, named queries and not SQL statements. As #Jiggles32 demonstrates, you need to create a named query and then reference it with QueryDefs() call.
However, you can bypass the use of queries by simply directly opening recordsets with OpenRecordset() which is the end result of your needs:
strSQL = "SELECT * FROM [tReg]"
Me.RecordSource = strSQL
Set rstReport = dbsReport.OpenRecordset(strSQL)
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
In fact, you can directly extract a form's recordset using RecordsetClone property (preferred over Recordset if running various operations to not affect form's actual records):
strSQL = "SELECT * FROM [tReg]"
Me.RecordSource = strSQL
Set rstReport = Me.RecordsetClone
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
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.
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
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.
I am working on an application that should allow user select a code from a list box, and this value should be passed as a parameter to a sub-form(continuous) which is tied to an SQL Query to populate the form with values returned from the query.
How do I tie the listbox on the parent form to the continuous form (child form) in such a way that a change in the listbox is registered in the subform and it(child form) is re-populated accordingly?
The code below is what I currently have in my Parent form to trigger the change in the subform. But I am still missing the bit where I trigger the reloading of the information in the subform with this.
storedproc
SELECT tblACCOUNT.ACCOUNT_LABEL, tblACCOUNTValue.[ACCOUNTVALUE_VALUE]
FROM ((tblUPS INNER JOIN tblProductUPS ON tblProductUPS.[PRODUCTUPS_UPS] = tblUPS.[UPS_CODE]) INNER JOIN tblACCOUNT ON tblUPS.UPS_ID = tblACCOUNT.ACCOUNT_UPSID) INNER JOIN tblACCOUNTValue ON tblACCOUNTVALUE.[ACCOUNTVALUE_ACCOUNTID]= tblACCOUNT.[ACCOUNT_ID]
WHERE TBLPRODUCTUPS.[PRODUCTUPS_PRODUCTID] = (SELECT tblProductLevel.[PRODUCTLEVEL_ID]
FROM tblProductLevel WHERE tblProductLevel.[PRODUCTLEVEL_Code] IN ( [UPSIDS])
);
This is a field in my sub-form which I would like to tie to my query as source of parameter for the query.
Public Property Let qString(unstring As String)
If Not IsNull(unstring) And Len(unstring) > 0 Then
Currentstring = unstring
End If
End Property
The code below is code in my parent form I test connects directly to the Query that I created and passes the parameter directly to the query. However, since my sub-form is itself tied to the query, I need a way to get to the subform query and pass the information I need passed and invoke an update on the form afterwards.
Private Sub LoadSubform(unspscstring As String)
On Error GoTo Err_LoadSubform_Change
Dim dbs As Database
Dim strSQL As String
Dim strSelect As String
Dim strQueryName As String
Dim qryDef As QueryDef
Dim rst As Recordset
Dim prmOne As DAO.Parameter
Set dbs = CurrentDb
'then we'll open up the query:
Set qryDef = dbs.QueryDefs("spgetAttributeByUNSPSC")
'Now we'll assign values to the query using the parameters option:
'link your DAP.Parameters to the query
'Set prmOne = qryDef.Parameters!param_one
'prmOne = unspscstring
qryDef.Parameters(0) = unspscstring
'Now need to somehow trigger an update on the subform
'Close all objects
rst.Close
qryDef.Close
Set rst = Nothing
Set qryDef = Nothing
Bye_LoadSubform_Change:
Exit Sub
Err_LoadSubform_Change:
Beep: MsgBox Error$, 16, "Select Failed"
Resume Bye_LoadSubform_Change
End Sub
As long as the listbox allows only one item, you can use the link child and master fields of the subform to do this.
Link Child Fields: ID; ListFieldMatch
Link Master Fields : ID; MyListBox
Alternatively, you can set the Record Source of the form object of the subform control in VBA:
sSQL = "SELECT ID,Stuff FROM Table WHERE ID=" & Me.MyNumericListBox
Me.MySubformControlName.Form.RecordSource = sSQL
sSQL = "SELECT ID,Stuff FROM Table WHERE ID=" & Me.MyNumericListBox
Me.MySubformControlName.Form.RecordSource = sSQL
gives an error 2467 runtime error
"The expression you entered refers to an object that is closed or does'nt exist.