So I have a bunch of tables all linked together someway or another, and I'm trying to make a report that lists all the items in a selected range of invoice numbers, and to begin the process, the User will click a button, and that button will open a pop up form with 3 fields, FirstInvoiceNumber, LastInvoiceNumber, and Client. These fields than save what the user enters as globals which are then plugged into the SQL statement in the Report_Open Event. There was a syntax error, but after about half an hour I found and fixed it. but now, I'm getting this Run-time Error 2465, which says "Microsoft Access can't find the field 'l1' (In front of the 1, its a symbol I don't know how to type, but it looks like a tall lowercase L) referred to in your expression."
Maybe I'm just looking at this for too long, but the only "1" I can see, is where the SQL statement has the Initials field, and both of them are inside parenthesis, so I'm not sure whats wrong. Anyways, here is this ridiculous SQL
(Note: I find it easier to read on one line and just scroll sideways rather than having a big chunk split onto multiple lines, so this SQL statement is fit onto only 2 lines, I'll put an extra paragraph space so it's easier to find it)
Me.RecordSource = "SELECT Invoices.InvNo,
Invoices.JobNo,
Invoices.InvDate,
Invoices.TimeTotal,
Invoices.Tax,
Invoices.POTotal,
Invoices.SubTotal,
Invoices.InvTotal,
Invoices.DatePaid,
Invoices.AmountPaid,
Invoices.Terms,
Contacts.EMail,
Invoices.Status,
' & 'Attn: ' & [FirstName] & ' ' & [LastName] & ' AS FullName,
Companies.CompanyFullName,
Companies.CompanyAbrv,
Contacts.Department,
' & [City] & ',
' & [State] & ' ' & [Zipcode] & ' AS AddressLine3,
Invoices.ClientPONo,
InvLineItems.LineItemNote,
InvLineItems.Adjustments,
InvLineItems.Status,
InvLineItems.Cost,
[Cost]+([Cost]*[Adjustments]) AS AAC,
' & [Contacts].[AddressLine1] & ',
' & [Contacts].[AddressLine2] & ' AS AddressLine1and2,
' & [Invoices].[InvNo] & '-' & [Invoices].[JobNo] & ' AS InvNoandJobNo,
Left([Contacts].[FirstName],
1) & Left([Contacts].[LastName],
1) AS Initials,
Jobs.JobTitle,
& ' & [Jobs].[JobTitle] & ' ' & [Jobs].[ClientCode] & ' AS Product,
InvLineItems.Requester,
Companies.CompanyID' _
& 'FROM ((((Invoices
LEFT JOIN (Contacts
RIGHT JOIN Jobs ON Contacts.ContactID = Jobs.ContactID) ON Invoices.JobNo = Jobs.JobNo)
LEFT JOIN InvLineItems ON Invoices.InvNo = InvLineItems.InvNo)
LEFT JOIN PurchaseOrders ON Invoices.InvNo = PurchaseOrders.InvNo)
LEFT JOIN POLineItems ON PurchaseOrders.PONo = POLineItems.PONo)
LEFT JOIN Companies ON Jobs.CompanyID = Companies.CompanyID'
Related
I have a table with 4 columns and update them through excel user form and all are varchar (255). when I try to enter the character ' I get syntax error, mysql doesn't accept it... What am I doing wrong here, do I need to change datatype
Update: I figured that the problem is not with MySQL (obviously :) ) but my code to update the table.
Dim sq As String
sq = "UPDATE sample.`nov-21` SET `Site work being carried out`='" & sitecombo.value & "',`Group`='" & eqgrp.value & "',`Description`='" & desc.value & "',`T Number`='" & tn.value & "', WHERE sample.`nov-21`.`ID`= " & Me.IDnum & ";"
Escape ' with \ or another ': 'O''Malley' or 'O\'Malley'
When you write 'O'Malley', MySQL read string literal 'O' followed by name Malley (which is not recognized) and a ' with no meaning, hence the syntax error.
I'm using Access 2007 by itself with no connections to SQLserver or anything for this process.
I want to take the result of a few DateDiff functions and use an Update SQL statement to put them into fields on a table. My table's fields are number fields, and I am under the impression that DateDiff returns a number.
I try this, but I get a data type mismatch error on the first DateDiff (Pause1). I tried taking the quotes off of the fields but then I get a different error (can't find the field '|' referred to in your expression).
Here is my code. It really starts at the comment TIME REPORTING CODE HERE:
Private Sub StopNextButton_Click()
'
GetID = Forms!frm_MainMenu!AssocIDBox
CurRecord = Forms!frm_EC_L1_L2![L#].Value
'
DoCmd.RunSQL "UPDATE tbl_Data SET tbl_Data.[tsEndAll] = Now WHERE tbl_Data.[L#] = " & CurRecord & " AND (tbl_Data.[ECName] Like 'L1*' OR tbl_Data.[ECName] Like 'L2*') "
'
'TIME REPORTING CODE HERE'
'
Pause1 = DateDiff("s", "[tsPause1]", "[tsResume1]")
Pause2 = DateDiff("s", "[tsPause2]", "[tsResume2]")
ECTime = (DateDiff("s", "[tsECStart]", "[tsUpdated]") - (Pause1 + Pause2))
LTime = DateDiff("s", "[tsStartAll]", "[tsEndAll]")
'
DoCmd.RunSQL "UPDATE tbl_Data SET [ECTime] = " & ECTime & ", [LoanTime] = " & LTime & " WHERE tbl_Data.[L#] = " & CurRecord & " AND (tbl_Data.[ECName] Like 'L1*' OR tbl_Data.[ECName] Like 'L2*') "
'
'END OF TIME REPORTING CODE'
'
DoCmd.GoToRecord , , acNext
'
ETC.
Based off of your comment I assume that those fields are on the record your form is currently 'viewing'. If so you can just refer to them as Me.tsPause1 without [] or quotes. Pretty sure you can also do just tsPause1 but I find Me.tsPause1 makes it more obvious what you are doing.
However I think you are updating the field you are currently viewing and then immediately trying to access those updated fields. I am fairly certain you will need to a Me.Refresh before those fields' new values are accessible. Hopefully someone with more specific experience will correct me if I am wrong. I think something like this should work for you:
Me.Refresh
Pause1 = DateDiff("s", Me.tsPause1, Me.tsResume1)
Pause2 = DateDiff("s", Me.tsPause2, Me.tsResume2)
ECTime = (DateDiff("s", Me.tsECStart, Me.tsUpdated) - (Pause1 + Pause2))
LTime = DateDiff("s", Me.tsStartAll, Me.tsEndAll)
I'm using Replace function in SQL query, it is working in access sql query design mode but not working in access VBA. Throwing expected end of statement. I tried all the ways but not working. CM column has a string like this ;#WR_1;#WR_2;#WR_3;#WR_4;# and I'm trying to get value WR_2 where ever the string occurs in CM column
strSQL = "SELECT * FROM WT_table " & _
"WHERE [CM] IS NOT NULL " & _
"AND (';' & Replace([CM], '#', "") & ';') Like '*;WR_2;*'; ;"
Just remove the second semi-colon (;) from the end of the last line - you have two of them, making it appear that there are multiple statements to be executed.
"AND (';' & Replace([CM], '#', "") & ';') Like '*;WR_2;*';"
In addition, as pointed out by simoco, you have a pair of double-quotes in your Replace function. I'm not certain whether you mean to have two single-quotes or not, but this would be the right code to use in that case:
"AND (';' & Replace([CM], '#', '') & ';') Like '*;WR_2;*';"
I have this SQL query
cmd.CommandText = "SELECT [tblStudent]![LastName] & ', ' & [tblStudent]![FirstName] & ' ' & [tblStudent]![MiddleName] AS FullName," & _
" tblGrade.FirstGrading, tblGrade.SecondGrading, tblGrade.ThirdGrading, tblGrade.FourthGrading," & _
" Avg(b.GradeValue) AS AverageGrade" & _
" FROM ((((tblGrade INNER JOIN tblEnrolment ON tblGrade.EnrolmentID=tblEnrolment.EnrolmentID)" & _
" INNER JOIN tblStudent ON tblStudent.StudentID=tblEnrolment.StudentID)" & _
" INNER JOIN tblSubjectOffering ON tblSubjectOffering.SubjectOfferingID=tblGrade.SubjectOfferingID)" & _
" INNER JOIN tblSubject ON tblSubject.SubjectID=tblSubjectOffering.SubjectID)" & _
" INNER JOIN tblSection ON tblSection.SectionID=tblSubjectOffering.SectionID" & _
" WHERE tblSection.SectionTitle='" & s(0) & "' AND tblSubject.SubjectID='" + s2(0) + "'" & _
" GROUP BY [tblStudent]![LastName] & ', ' & [tblStudent]![FirstName] & ' ' & [tblStudent]![MiddleName], tblGrade.FirstGrading, tblGrade.SecondGrading, tblGrade.ThirdGrading, tblGrade.FourthGrading" & _
" ORDER BY [tblStudent]![LastName] & ', ' & [tblStudent]![FirstName] & ' ' & [tblStudent]![MiddleName]"
And this code gives me a runtime error "No value for one or more required parameters".
My hunch is Access thinks b.GradeValue is a parameter because there is no table name or alias for b:
Avg(b.GradeValue) AS AverageGrade
I suggest you Debug.Print the completed SELECT statement, or write it to a text file, then test it as a new query in the Access query designer. When it asks you to supply the parameter value, it also shows you the "name" of whatever it thinks is the parameter.
Very difficult to offer suggestions with only a SQL statement, out of context with your other code and objects.
As it stands, I see two main potential problems. First is that you have Avg(b.GradeValue) but there is no reference to the b table elsewhere in the command. For this, you would need to remove the b or make sure the reference is valid.
Second, you have two variables, s() and s2() that could be returning invalid or null data. You may want to debug this step and check the values each of these holds before letting it proceed, then fix the problem in the other part of your code if one is bad.
just wondering when using an expression on a form in a text box, to return a value from a table, can the expression have multiple tables in the expression to return the value?
the tables are linked and I can return the value in a query, so I figured that Access would be able to do it with this method as well????
=DSum("[tblMain]![Revenue]","tblMain","[tblMain]![Quarter]=3 AND [tblMain]![Region]='NorthEast'" AND [tblOffice]![Location]='NewYork'")
this is the expression that I entered into my text box, without the reference to the 2nd table it works fine, but once I had it, I get the flickering error message in the text box (just as on a report)......
I know this method is probably used more in reports than forms, but I am novice, and trying to come up with a dashboard solution that returns lots of facts quickly per department. I am using this in the "Control Source" field of the data tab of the properties window, not VB. Mainly because I do not know how to get it to work with VB.
Thanks for the help as always!
As far as I know, you cannot refer to more than one table or query in a domain aggregate function. As grazird says, how are these tables related? Let us say it is on tblMain ID, you can build a query called, say, qryMainOffice, the SQL (SQL View, Query Design window) would look something like:
SELECT [tblMain].[Revenue],[tblMain]![Quarter],[tblMain]![Region],
[tblOffice]![Location]
FROM tblMain
INNER JOIN tblOffice
ON tblMain.ID = tblOffice.MainID
DSum would then be (remove line break):
=NZ(DSum("[Revenue]","qryMainOffice",
"[Quarter]=3 AND [Region]='NorthEast' AND [Location]='NewYork'"),"Not found")
You could also use a recordset or query in VBA to return the value.
EDIT re COMMENT
To use the above in VBA, you either need to add parameters or use a string:
''Reference: Microsoft DAO 3.x Object Library
Dim rs As DAO.Recordset
Dim db As Database
Dim strSQL as String
Set db= CurrentDB
strSQL = "SELECT Sum(t.[Revenue]) As TotalNY" _
& "FROM tblMain t " _
& "INNER JOIN tblOffice o " _
& "ON t.ID = o.MainID " _
& "WHERE t.[Quarter]=3 AND t.[Region]='NorthEast' " _
& "AND o.[Location]='NewYork' " _
'' I have use aliases for simplicity, t-tblMain, o-tblOffice
'' If you wish to reference a control, use the value, like so:
'' & " AND [Location]='" & Me.txtCity & "'"
'' Dates should be formated to year, month, day
'' For joins, see http://www.devshed.com/c/a/MySQL/Understanding-SQL-Joins/
Set rs = db.OpenRecordset strSQL
If Not rs.EOF Then
Me.txtAnswer = rs!TotNY
Else
Me.txtAnswer = "N/A"
End If
You can also use different queries to return several results that can be shown with a list box or a subform:
strSQL = "SELECT TOP 5 o.[Location]," _
& "Sum(t.[Revenue]) AS TotRevenue" _
& "FROM tblMain t " _
& "INNER JOIN tblOffice o " _
& "ON t.ID = o.MainID " _
& "WHERE t.[Quarter]=3 AND t.[Region]='NorthEast' " _
& "GROUP BY o.[Location]"
The above would return revenue for quarter 3 for all locations in NorthEast region. If you want the top values of each group, you are looking at a more complicated query, which I will leave for now.
How are these tables related? Can you describe the relationship and any primary/foreign keys?
Also, referencing the table name is not necessary in the first parameter of this function (since it is already taken care of in the second one).
For example, your code could be:
=DSum("Revenue","tblMain","Quarter=3 AND Region='NorthEast'" AND [tblOffice]![Location]='NewYork'")
Just trying to save you some keystrokes and increase its readability. :)