Hello All,
Long time lurker, first time poster. This is a great site full of information and guides. I use this alot for research on errors and general information on what I am doing wrong.
Thank you for all of the contributors that help everyone out!
But I am here because I could not find an answer for my problem, which should be pretty easy but I'm not experienced enough to see it clearly; or I should maybe step away for a little bit.
Overview: I have a main table of customer names,accounts, countries, etc etc. I have a created a form that has a list box and a combo box inside. The combo box stores all of the customer names and allows the user to select one. The list box stores the associated countries with each customer name. The idea is to put insert all of the account numbers into a table that match customer name and their respective country.
I have set this up through a button that runs on the first click. I have two different ways of coding this, both with small errors.
This one returns an error stating it could not could not find the field "|" in the expression:
Code:
Private Sub but_add_accounts_Click()
Dim Result As Variant
Dim strSql As String
Dim StrWhere As String
'Clean old accounts first
DoCmd.RunSQL "DELETE * FROM AccountsForForms"
'Matches data selected from Combo/List box in the form to the table with accounts
'ParentNameAndAccounts = Form name. ParentNames = Combobox name inside the Form. AccountList = Listbox name inside the Form
StrWhere = [Parent_Accounts].[P_Name] = Me.ParentNameAndAccounts.[ParentNames] And [Parent_Accounts].[AC_Cntry] = Me.ParentNameAndAccounts.[AccountList]
Result = DLookup("[AC_NR]", "[Parent_Accounts]", " & StrWhere & ")
'Insert the matches account #'s into the new seperate account table named: AccountsForForms.
strSql = "INSERT INTO AccountsForForms ([ACCOUNTS]) " & " VALUES('" & varResult & "')"
Debug.Print strSql
DoCmd.RunSQL strSql, dbFailOnError
Beep
MsgBox "Accounts Added", vbInformation
End Sub
If I use this slightly altered version, I get no errors but it returns nothing into the table. Using the debugger/step-into method I can see it is returning null on the criteria of where it states AC_Cntry = Account list. But I can not figure out why.
Code:
Private Sub but_add_accounts_Click()
Dim Result As Variant
Dim strSql As String
'Clean old accounts first
DoCmd.RunSQL "DELETE * FROM AccountsForForms"
'Goal is to Match data selected from Combo/List box in the form to the table with accounts
'ParentNameAndAccounts = Form name. ParentNames = Combobox name inside the Form. AccountList = Listbox name inside the Form
'Insert the matches account #'s into the new seperate account table named: AccountsForForms.
Result = DLookup("[AC_NR]", "Parent_Accounts", P_Name = [Forms]![FrmPOSTCOST]![ParentNameAndAccounts]![ParentNames] And AC_Cntry = [Forms]![FrmPOSTCOST]![ParentNameAndAccounts]![AccountList])
strSql = "INSERT INTO AccountsForForms ([ACCOUNTS]) " & " VALUES('" & varResult & "')"
Debug.Print strSql
DoCmd.RunSQL strSql, dbFailOnError
Beep
MsgBox "Accounts Added", vbInformation
End Sub
Any help or guidance would be greatly appreciated!