I redid the search code and wrote it as a vba code instead of a query, but now I am running into errors now that it is all setup. The code is below:
The code I am getting is OBJECT REQUIRED 424 and it is highlighting the
If Me!txtEmpID Is Not Null Then
Private Sub cmdSearch2_Click()
Dim strSQL As String
strSQL = ""
If Me!txtEmpID Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[EmployeeID] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!txtEmpName Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[EmployeeName] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboEEOC Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[EEOC] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboGender Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Gender] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!txtDivision Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Division] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboRR Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Region] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboDD Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[District] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboJobGroupCode Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[JobGroupCOde] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!txtCenter Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Center] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!txtJobD Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[JobDesc] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboJobGroup Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[JobGroup] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboFunction Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Function1] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboMtgReadyLvl Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[MeetingReadinessRating] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboMgrReadyLvl Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[ManagerReadinessRating] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!txtFeedback Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[EmployeeFeedback] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboDevelopment1 Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment1 & " * ') "
End If
If Me!cboDevelopment2 Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment2 & " * ') "
End If
If Me!cboDevelopment3 Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment3 & " * ') "
End If
If Me!cboDevelopment4 Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment4 & " * ') "
End If
If Me!cboDevelopment5 Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment5 & " * ') "
End If
If Me!txtJustification Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Justification] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!txtNotes Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Notes] LIKE '*" & Me!txtEmpID & " * ' "
End If
If Me!cboChanged Is Not Null Then
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _
"[Changed] LIKE '*" & Me!txtEmpID & " * ' "
End If
strSQL = "SELECT * FROM [CDData] WHERE " & strSQL
DoCmd.RunSQL strSQL
End Sub
Checking for null in If statements is a little different:
If Not IsNull(Me!txtEmpID) Then
And so on.
Related
I'm trying to add to the following query:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
A row that makes the sum of the fldValue like:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"UNION " & _
"SELECT Sum(fldValue) AS fldValue " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
the error is:
Run -time error '3141'. The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect
I found this is working:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION " & _
"SELECT '' AS fldName, 'Total' AS Total, Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2 AS B " & _
"WHERE " & _
"B.isin='" & Code & "' " & _
"AND " & _
"B.fldName='" & fldName & "' "
This should run as expected:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2"
If you have Null values, use Nz:
strSQL = "SELECT fldName, blkName, CDbl(Nz(fldValue, 0)) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(Nz(fldValue, 0))) " & _
"FROM dbSecurities2"
The following code produces an error (Method 'Open' of object'_Recordset' failed) when I get to the statement that tries to open the recordset:
Set objClubSalesSourceConn = CreateObject("ADODB.COnnection")
objClubSalesSourceConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDirectory & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
Set rsClubOrders = CreateObject("ADODB.Recordset")
strSQL = "SELECT"
strSQL = strSQL & vbCrLf & " ClubSales.[Order Number] AS OrderNum,"
strSQL = strSQL & vbCrLf & " ClubSales.[Submitted Date] AS SaleDate,"
strSQL = strSQL & vbCrLf & " ClubSales.[Product SKU] AS SKU,"
strSQL = strSQL & vbCrLf & " ClubSales.[Product Name] AS ItemDesc,"
strSQL = strSQL & vbCrLf & " ClubSales.[Ext Item Price] AS SaleAmt,"
strSQL = strSQL & vbCrLf & " ClubSales.[Ext Item Shipping] AS ShipAmt,"
strSQL = strSQL & vbCrLf & " CASE WHEN ISNULL(ClubSales.[Ship Date])"
strSQL = strSQL & vbCrLf & " THEN 1"
strSQL = strSQL & vbCrLf & " ELSE 0 END AS ShipDateNull,"
strSQL = strSQL & vbCrLf & " ClubSales.[Ship Date] AS ShipDate,"
strSQL = strSQL & vbCrLf & " ClubSales.[Pickup Date] AS PickupDate,"
strSQL = strSQL & vbCrLf & " ClubSales.[Quantity Sold] * ClubSales.[Cost Of Goods] AS COGSAmt"
strSQL = strSQL & vbCrLf & "FROM"
strSQL = strSQL & vbCrLf & " ClubSalesSource.csv AS ClubSales"
rsClubOrders.Open strSQL, objClubSalesSourceConn, adOpenDynamic*
The problem lies in the CASE WHEN statement in the SQL. If I leave that out, the recordset opens okay. I am in Excel VBA, querying a csv file. I need to know how to fix this. I have also unsuccessfully tried using an IIF function like I have done in Access.
strSQL = "SELECT" & _
" ClubSales.[Order Number] AS OrderNum," & _
" ClubSales.[Submitted Date] AS SaleDate," & _
" ClubSales.[Product SKU] AS SKU," & _
" ClubSales.[Product Name] AS ItemDesc," & _
" ClubSales.[Ext Item Price] AS SaleAmt," & _
" ClubSales.[Ext Item Shipping] AS ShipAmt," & _
" CASE WHEN ISNULL(ClubSales.[Ship Date])" & _
" THEN 1" & _
" ELSE 0 END AS ShipDateNull," & _
" ClubSales.[Ship Date] AS ShipDate," & _
" ClubSales.[Pickup Date] AS PickupDate," & _
" ClubSales.[Quantity Sold] * ClubSales.[Cost Of Goods] AS COGSAmt" & _
" FROM" & _
" ClubSalesSource.csv AS ClubSales"
My code is exiting out of the sub on the line that says "CurrentDb.Execute strSQL_Insert_Data". Do you know why this is happening?
Local_Array = Array("dbo_Tape_Capture_Local_tbl", "dbo_Tape_Local_tbl", "dbo_Tape_Memo_Local_tbl")
Server_Array = Array("dbo_Tape_Capture", "dbo_Tape", "dbo_Tape_Memo")
For i = 0 To UBound(Local_Array)
strSQL_Insert_Data = "INSERT INTO [" & Local_Array(i) & "] " & _
"SELECT [" & Server_Array(i) & "].* " & _
"WHERE (LEFT([" & Server_Array(i) & "].header__situs_loan_id," & _
Len([Forms]![Login Page]![CBO_Job_Select_Login]) & ") = " & _
"[Forms]![Login Page]![CBO_Job_Select_Login]);"
CurrentDb.Execute strSQL_Insert_Data
CurrentDb.Close
Next i
--Added Printed out code--
INSERT INTO [dbo_Tape_Capture_Local_tbl] SELECT [dbo_Tape_Capture].*
WHERE (LEFT([dbo_Tape_Capture].header__situs_loan_id,14) = [Forms]![Login Page]![CBO_Job_Select_Login]);
There is missing FROM TableName in your query. Replace TableName with Actual Table Name
strSQL_Insert_Data = "INSERT INTO [" & Local_Array(i) & "] " & _
"SELECT [" & Server_Array(i) & "].* " & _
"FROM [" & Server_Array(i) & "] " & _
"WHERE (LEFT([" & Server_Array(i) & "].header__situs_loan_id," & _
Len([Forms]![Login Page]![CBO_Job_Select_Login]) & ") = " & _
"[Forms]![Login Page]![CBO_Job_Select_Login]);"
I'm attempting to write a domain function I can use in MS Access SQL view like what is found here: DMedian in access 2013, no values returned
...but for a DPercentile function that allows you to GROUP BY and filter.
This is what I have so far:
Public Function DPercentileWithGrpBy( _
ByVal sFld As String, _
ByVal sTable As String, _
ByVal iPercent As Integer, _
ByVal sGrpByFld As String, _
ByVal sGrpByValue As String _
) As Variant
Dim db As DAO.Database
Dim rstDomain As DAO.Recordset
Dim sSQL As String
Const errAppTypeError = 3169
On Error GoTo HandleErr
Set db = CurrentDb()
' Build SQL string for recordset.
sSQL = "SELECT " & _
sTable & "." & sGrpByFld & _
"," & (1 - iPercent / 100) & "*(" & _
"SELECT Max(" & sFld & ") " & _
"FROM " & sTable & " " & _
"WHERE " & sTable & "." & sFld & " IN (" & _
"SELECT TOP " & iPercent & " PERCENT " & sFld & " " & _
"FROM " & sTable & " " & _
"WHERE " & sTable & "." & sGrpByFld & " = " & Chr(34) & sGrpByValue & Chr(34) & " AND " & sFld & " Is Not Null ORDER BY " & sFld & ")) + " & iPercent / 100 & "*(" & _
"SELECT Min(" & sFld & ") " & _
"FROM " & sTable & " " & _
"WHERE " & sTable & "." & sFld & " IN (" & _
"SELECT TOP " & 100 - iPercent & " PERCENT " & sFld & " " & _
"FROM " & sTable & " " & _
"WHERE " & sTable & "." & sGrpByFld & " = " & Chr(34) & sGrpByValue & Chr(34) & " AND " & sFld & " Is Not Null ORDER BY " & sFld & " DESC)" & _
") AS " & iPercent & "Percentile " & _
"FROM " & sTable & " " & _
"WHERE " & sTable & "." & sGrpByFld & " = " & Chr(34) & sGrpByValue & Chr(34) & " " & _
"GROUP BY " & sTable & "." & sGrpByFld & ";"
'Debug.Print sSQL
'above should result in something like this:
'SELECT
' tblFirst250.[GICS Sector]
' , 0.75*(
' SELECT Max(GM)
' FROM tblFirst250
' WHERE tblFirst250.GM IN (
' SELECT TOP 25 PERCENT GM
' FROM tblFirst250
' WHERE tblFirst250.[GICS Sector] = "Energy" AND GM Is Not Null ORDER BY GM)) + 0.25*(
' SELECT Min(GM)
' FROM tblFirst250
' WHERE tblFirst250.GM IN (
' SELECT TOP 75 PERCENT GM
' FROM tblFirst250
' WHERE tblFirst250.[GICS Sector] = "Energy" AND GM Is Not Null ORDER BY GM DESC)
' ) AS 25Percentile
'FROM tblFirst250
'WHERE tblFirst250.[GICS Sector] = "Energy"
'GROUP BY tblFirst250.[GICS Sector];
Set rstDomain = db.OpenRecordset(sSQL, dbOpenDynaset)
DPercentileWithGrpBy = rstDomain
ExitHere:
On Error Resume Next
rstDomain.Close
Set rstDomain = Nothing
Exit Function
HandleErr:
' Return an error value.
DPercentileWithGrpBy = CVErr(Err.Number)
Resume ExitHere
End Function
I'd like to have the ability to use the function in MS Access SQL View as a query or part of a query. I also need to do a percentile (quartiles) of a sub group of records. Hopefully that make sense...
EDIT: the resulting query works when I debug.print it and using it in SQL View.
EDIT: here is how you could use it:
DPercentileWithGrpBy( "GM","tblFirst250", 25,"[GICS Sector]","Energy")
hello i wrote this code to open a report based on a query
and this query is based on the toggle button
the problem is if i press one toggle button all work
but if i press more than toggle button in the same time it will not give me the right records or it will give me an empty report
this is the code
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tbl_Mouzakarat"
Dim p_7abes As String
Dim p_gharame As String
Dim p_done As String
Dim p_undone As String
Dim p_khoulasa As String
Dim p_mouzakara As String
Dim p_karar As String
Dim p_jaze2e As String
Dim p_lebanese As String
Dim p_foreign As String
Dim p_SQL_criteria As String
p_7abes = Trim(Me!text_7abes & " ")
p_gharame = Trim(Me!text_gharame & " ")
p_done = Trim(Me!text_done & " ")
p_undone = Trim(Me!text_undone & " ")
p_khoulasa = Trim(Me!text_khoulasa & " ")
p_mouzakara = Trim(Me!text_mouzakara & " ")
p_karar = Trim(Me!text_karar & " ")
p_jaze2e = Trim(Me!text_jaze2e & " ")
p_lebanese = Trim(Me!text_lebanese & " ")
p_foreign = Trim(Me!text_lebanese & " ")
If p_7abes <> "" Then
p_SQL_criteria = "[Punish]" & " LIKE '" & p_7abes & "'"
End If
If p_gharame <> "" Then
p_SQL_criteria = "[Punish]" & " LIKE '*" & p_gharame & "*'"
End If
If p_done <> "" Then
p_SQL_criteria = "[Status_Check]" & " LIKE '*" & p_done & "*'"
End If
If p_undone <> "" Then
p_SQL_criteria = "[Status_Check]" & " LIKE '*" & p_undone & "*'"
End If
If p_khoulasa <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_khoulasa & "*'"
End If
If p_mouzakara <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_mouzakara & "*'"
End If
If p_karar <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_karar & "*'"
End If
If p_jaze2e <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_jaze2e & "*'"
End If
If p_lebanese <> "" Then
p_SQL_criteria = "[Nationality]" & " LIKE '*" & p_lebanese & "*'"
End If
If p_foreign <> "" Then
p_SQL_criteria = "[Nationality]" & " NOT LIKE '*" & p_foreign & "*'"
End If
If Me.chk7abes.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkGharame.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkDone.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.ChkUndone.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKhoulasa.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkMouzakara.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKarar7abes.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKararJaze2e.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
DoCmd.OpenReport "rpt_Mouzakarat", acViewPreview
DoCmd.Close acForm, "frm_Printing"
will someone check the code please
You are overwriting p_SQL_criteria each time then running the same exact query each time no matter how many toggle buttons are pressed.
"INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
This is always the exact same SQL statement for each of your DoCmd statements.
I'm not really sure how your toggle buttons interact with the parameters you are trying to create so it's hard to suggest solutions, but this is why your having the problem you are having.