This is pretty easy question, but I have been unable to find the solution using google. More than likely I am using the wrong terminology. Any help in addressing my issue or pointing me in the right direction would be greatly appreciated. Due an in house requirement at my company, I am creating an Access database with an ODBC connection to SQL Server to maintain a user defined table. It has been a while since I have programmed in VBA.
I am using an Access Form to pull up the data to be edited. With a button press, a query runs and brings back one row based on the criteria. I am using a record set to hold the information pulled back from the query. My goal is to then put the data from the query into text boxes where they can be edited, then do an update statement with the changes made. However, the code I wrote to put the record set data into the various text boxes is failing and I’m not able to figure out why.
Here is my code:
Dim rst As Recordset
'add rows to record set
Set rst = CurrentDb.OpenRecordset("Select Parent_Name, Child_Name, Address, City, State, Zip, CustomerID, Facility, SystemID, " & _
"from Table where CustomerID = '" & Forms!Frm_Edit_Child!cbx_Cust_ID & "' and Facility = '" & Forms!Frm_Edit_Child!cbx_Facility & "' and SystemID = '" & _
Forms!Frm_Edit_Child!cmb_SystemID & "';")
rst.MoveLast
Me.txt_Edit_IDN_Name.Text = rst.Fields("Parent_Name").Value
Me.txt_Edit_Customer_ID.Text = rst.Fields("CustomerID").Value
Me.txt_Edit_Facility.Text = rst.Fields("Child_Name").Value
Me.txt_Edit_Facility_Num.Text = rst.Fields("Facility").Value
Me.txt_Edit_Address.Text = rst.Fields("Address").Value
Me.txt_Edit_City.Text = rst.Fields("City").Value
Me.cmb_Edit_State.Text = rst.Fields("State").Value
Me.txt_Edit_Zip.Text = rst.Fields("Zip").Value
Me.cmb_Edit_SystemID.Text = rst.Fields("SystemID").Value
I think the way I am attempting to reference the data in the record set is wrong, but I’m not able to figure out where I went wrong or use the proper terminology to find a solution via Google. Thank you for your time!
I think you have multiple issues with that code. Before discussing them I will offer you this advice. Telling us your code fails is nowhere near as useful as telling us “
CurrentDb.OpenRecordsettriggers error #3141, ‘The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.'”Discard the comma after
SystemIDin yourSELECTstatement field list. The last field expression can’t be followed by a comma or you will trigger error #3141.Store your
SELECTstatement in a string variable, thenDebug.Printit before you use it to open the recordset.Then if you still encounter an error on
CurrentDb.OpenRecordset, you can go to the Immediate window (CTRL+g) to view the statement you’re asking the db engine to use. You may spot the problem as soon as you see the actual completed statement. If not, you can copy the statement and paste it into SQL View of a new query … figure out what changes are needed to avoid the error, then revise your VBA code so that it produces the same correct SQL statement. If that still doesn’t fix you up, paste the SQL into your question and tell us what error message you get from that SQL.Tableis a reserved word. See Problem names and reserved words in Access. If you must keep that as the table name, enclose it in square brackets to avoid confusing Access’ db engine.There could be a problem with this next line if your application includes a reference for Microsoft ActiveX Data Objects and it has a higher precedence than DAO.
It’s safer to explicitly declare the type of recordset you want.
Drop
.Textfrom lines like these as @user247245 suggested. The.Textproperty is only available for the control which currently has focus.You want
.Valueinstead. You can add it, but you don’t need to because.Valueis the default property. So these 2 are functionally equivalent:Finally, I think this situation would be much simpler with a form bound to the
SELECTstatement. You wouldn’t have to fill your text box values because Access will do that for you automagically with bound controls. You could revise the form’s Record Source based oncbx_Cust_ID,cbx_Facility, andcmb_SystemIDin the after update events of those controls or from the click event of a command button. If the code you showed us is from a different form (notFrm_Edit_Child), you would have to use the command button approach.