Vb.Net Search between two dates ms access - vb.net

I'm using vb.net Windows Form Application, and I have two datepicker and I want to filter between two dates...
I've tried this code :
Me.DBBindingSource.Filter = "[Data ardhjes] BETWEEN " & dtpDataArdhjes.Value.Date & " AND " & dtpDataArdhjesNE.Value.Date
And I got this error:
Additional information: The expression contains unsupported operator
'Between'.
and some other codes but it does not work..
[Data ardhjes] is on table, and dtpDataArdhjes is datetimepicker from the table
and dtpDataARdhjesNe is just a dateTimepicker, sa I want to search between two dates in same column named [Data Ardhjes]
any suggestion please ?

Try this :
Me.DBBindingSource.Filter = "[Data ardhjes] BETWEEN " & dtpDataArdhjes.Value.ToString("#yyyy/MM/dd#") & "
AND " & dtpDataArdhjesNE.Value.ToString("#yyyy/MM/dd#")

I got the answer:
Me.DBBindingSource.Filter = String.Format("[Data ardhjes] >= #{0:M/dd/yyyy}# AND [Data ardhjes] <= #{1:M/dd/yyyy}#", _
dtpDataArdhjes.Value, _
dtpDataArdhjesNE.Value)
It works great now..

Related

The SELECT statement includes a reserved word

So when I try to run my code I get a Runtime Error '3141' "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect."
Here is my code
strSQL5 = " SELECT DatePart('m',[gs_MultiList_Daily/monthly]![Test_Date]) AS Char, MonthName(DatePart('m',[Test_Date])) AS [Month], DatePart('yyyy',[Test_Date]) AS [Year], Sum([gs_MultiList_Daily/monthly].LeaksLogged) AS LeaksLogged, Sum([gs_MultiList_Daily/monthly].[ME Samples]) AS [ME Samples], Sum([gs_MultiList_Daily/monthly].MSampleLogged) AS MSampleLogged, Sum([gs_MultiList_Daily/monthly].Saddle) AS Saddle, Sum([gs_MultiList_Daily/monthly].EntranceDia) AS EntranceDia, Sum([gs_MultiList_Daily/monthly].TappingTee) AS TappingTee, [gs_MultiList_Daily/monthly].LDIW, [gs_MultiList_Daily/monthly].[X-Ray], [gs_MultiList_Daily/monthly].[ETSP Inspection], [gs_MultiList_Daily/monthly].DFT, [gs_MultiList_Daily/monthly].CDT, Sum([gs_MultiList_Daily/monthly].[Mount Photos]) AS [Mount Photos], Sum([gs_MultiList_Daily/monthly].[Melt Index]) AS [Melt Index], Sum([gs_MultiList_Daily/monthly].PSampleLogged) AS PSampleLogged, Sum([gs_MultiList_Daily/monthly].Density) AS Density, " & _
" Sum([gs_MultiList_Daily/monthly].PE_WT) AS PE_WT, Sum([gs_MultiList_Daily/monthly].OOR) AS OOR, " & _
" Sum([gs_MultiList_Daily/monthly].Poly_OD) AS Poly_OD, Sum([gs_MultiList_Daily/monthly].[WaterBath Out]) AS [WaterBath Out], Sum([gs_MultiList_Daily/monthly].[WaterBath In]) AS [WaterBath In], Sum([gs_MultiList_Daily/monthly].FTIR) AS FTIR, Sum([gs_MultiList_Daily/monthly].OIT) AS OIT, Sum([gs_MultiList_Daily/monthly].[Steel WT]) AS [Steel WT], Sum([gs_MultiList_Daily/monthly].[Steel Hardness]) AS [Steel Hardness], Sum([gs_MultiList_Daily/monthly].[Steel OD]) AS [Steel OD], Sum([gs_MultiList_Daily/monthly].[TIMP Corrosion]) AS [TIMP Corrosion], Sum([gs_MultiList_Daily/monthly].[Chem Analysis]) AS [Chem Analysis], Sum([gs_MultiList_Daily/monthly].[Steel Mounts]) AS [Steel Mounts], Sum([gs_MultiList_Daily/monthly].[Mounts Polished]) AS [Mounts Polished], Sum([gs_MultiList_Daily/monthly].Etch) AS Etch FROM [gs_MultiList_Daily/monthly] " & _
" GROUP BY DatePart('m',[gs_MultiList_Daily/monthly]![Test_Date]), MonthName(DatePart('m',[Test_Date])), DatePart('yyyy',[Test_Date]) " & _
" ORDER BY DatePart('yyyy',[Test_Date]) "
This is exactly how it is displayed on the window. The SELECT statement is really long and did not fit in a single line so I broke it up into 3 using " & _ " I also have spaces at both ends of the " so im clueless to where the error is coming from. Can someone please direct me to the correct path ? thank you!
CHAR is a reserved word, don't use it as an alias (or add [] brackets, but really, don't use it).
Generally, when you encounter this error, run each field name through the list of reserved words.

Dlookup reference where name of column being searched is the name of the textbox on my form

I am editing a database created by my predecessor at work. I am creating a "helper" textbox that will pull a value from a table in the same database.
Problem is, in my Dlookup, the name of the column that I am searching is also the name of the textbox on my form that contains the criteria. To change the name of my textbox, I would have to update a lot of code that I did not create. Is there a way around this?
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# = GreigeRoll#")
I expect the output to be the "GreigeWeightAvg" value from the table.
The output is:
"Syntax error (missing operator) in query expression 'GreigeRoll# = GreigeRoll#'."
Try to concatenate the value:
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "[GreigeRoll#] = " & Me![GreigeRoll#].Value & "")
or, it text:
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "[GreigeRoll#] = '" & Me![GreigeRoll#].Value & "'")
Include the fully-qualified control name in your selection criteria, e.g.:
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# = [Forms]![YourFormName]![GreigeRoll#]")
Change YourFormName as appropriate.
At first stop using special characters in field Names...some might think that it improves readability..BUT it won't...its almost a recipe for issues.
So ... just clarify what kind of value is GreigeRoll#
If its numeric (like 1,21,21321) then you should have :
txtgreigeweight = DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# =" & [GreigeRoll#])
On the other hand is alphanumeric (like "A12", "BigGreige","1stG") then it should be :
txtgreigeweight = DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# ='" & [GreigeRoll#] & "'")

Run time error in spreadsheet

thank you for your time. I've put stars on the error line if this helps.
I'm lost with an error and I can't find the reason. Can I get some help, please? The macros is not running for only one period and I didn't find any different data in the database compare to the previous period
That's the debug menu:
Get values and convert to string for text box:
a = Format(.Cells(rowNum, dateCol), "dd mmm")
a = a + ", " + Str(.Cells(rowNum, actualAssetCol))
Get values and convert to string for text box:
a = Format(.Cells(rowNum, dateCol), "dd mmm")
a = a + ", " + Str(.Cells(rowNum, actualAssetCol))`
The string concatenation operator in vba is & and not +. Thus, use it like this:
a = a & ", " & Str(.Cells(rowNum, actualAssetCol))`

VBA Dlookup not selecting items from table where certain dates are selected from drop down

I'm creating a database using MS Access, that allows a user to select a person and a week and view the shifts that person is going to do.
Here's my problem, I've got the code working using a DLookup to search for the staff member using the pid, and searching for a particular day as follows:
'Look up shifts
If IsNull(DLookup("Monday", "tblShifts", "[SPID]= " & SPID)) Then
MsgBox "Sorry missing shifts", vbCritical, "Error"
Else
selectedDate = Form_frmMain.cboWeekCommencing.Value
'MsgBox selectedDate 'shows selected date
MonShift = DLookup("Monday", "tblShifts", "[SPID]= " & SPID & " AND [WeekBegin]= #" & selectedDate & "#")
TueShift = DLookup("Tuesday", "tblShifts", "[SPID]= " & SPID & " AND [WeekBegin]= #" & selectedDate & "#")
WedShift = DLookup("Wednesday", "tblShifts", "[SPID]= " & SPID & " AND [WeekBegin]= #" & selectedDate & "#")
ThurShift = DLookup("Thursday", "tblShifts", "[SPID]= " & SPID & " AND [WeekBegin]= #" & selectedDate & "#")
FriShift = DLookup("Friday", "tblShifts", "[SPID]= " & SPID & " AND [WeekBegin]= #" & selectedDate & "#")
SatShift = DLookup("Saturday", "tblShifts", "[SPID]= " & SPID & " AND [WeekBegin]= #" & selectedDate & "#")
End If
Image of form being used in the database:
Selection of table used in DLookup:
From the above image, the date drop-down is populated from the following code on the form load:
thisWeek = Date - Weekday(Date, vbMonday) + 1
nextWeek = Date - Weekday(Date, vbMonday) + 8
weekAfter = Date - Weekday(Date, vbMonday) + 15
cboWeekCommencing.AddItem (thisWeek)
cboWeekCommencing.AddItem (nextWeek)
cboWeekCommencing.AddItem (weekAfter)
These each relate to week commencing 22/05/2017, 29/05/2017, 05/06/2017. When either the list box or drop-down box are changed the top function is called to populate the shifts, however when I select the weeks of either 22/05/2017 and 29/05/2017 it works fine, but when 05/06/2017 is selected the DLookup returns NULL values.
A few things I've tried are changing the weeks to the later weeks, again it works fine if the date is set to 19/06/2017, or 26/06/2017 but again doesn't work when the 12/06/2017 is used as the date.
Everything works fine when just the staff ID is used.
Does anyone have any idea why this might happen?
That's because Access is interpreting your dates wrong. Dates in Access SQL (and thus DLookUp) should be US format. Since 22/05/2017 in US would be the 22th month, and that doesn't exist, Access guesses that is European date format. But it should always be US format, so 05/06/2017 will be interpreted as the 6th of May (see Allen Browne's documentation).
So change this:
selectedDate = Form_frmMain.cboWeekCommencing.Value
to this:
selectedDate = Format$(Form_frmMain.cboWeekCommencing.Value, "\#mm\/dd\/yyyy\#")
Assuming that cboWeekCommencing is Date/Time type.
When you have written #XX/YY/ZZZZ# expression in the MS Access SQL query. MS Access tries to recognize a date. At first, MS Access try to recognize a date in the #MM/DD/YYYY# format. And only at unsuccessful attempt it try to recognize a date in the #DD/MM/YYYY# format.
In your example 19/06/2017 is recognized as 19 June 2017, but 05/06/2017 is recognized as 06 May 2017.
I recommend that you use the #MM/DD/YYYY# format every time in the MS Access SQL query.

Using a form control as a field selector in SQL query

I am attempting to build a form ,called UI, that users will select a dimension parameter from a combobox "cmbFilter" and then add a +/- tolerance in a text box "txtTolerance". After selection a part number from a list this should return results for similar part numbers in the the tolorence range for that parameter. The field names in the table are the dimension parameters and are .AddItem to the combobox in the form load code.
Example. Part#1 OD is 5, so I select "OD" as the search parameter then I set a tolerance to +/- 1. The results should show Part#2 with a OD of 6 but not Part#3 with a OD of 7.
I have set a listboxs row source to the query but
no matter what I change in the syntax in this code I get operation or syntax errors. So I assume Im not referencing the form control right, or my logic isn't right?
I have tired the following code in the SQL design view in access.
SQL
SELECT Part_Matrix.Part_Number, Part_Matrix.Customer, Part_Matrix.Large_OD, Part_Matrix.Vent_Opening, & _
Part_Matrix.BPT, Part_Matrix.MFT, Part_Matrix.PD, Part_Matrix.Hat_ID, Part_Matrix.Microfinish, & _
Part_Matrix.Turn_Operations, Part_Matrix.Stud_Holes, Part_Matrix.SH_Dimensions, Part_Matrix.Manufacturer_Holes, & _
Part_Matrix.MH_Dimensions, Part_Matrix.Other_Holes, Part_Matrix.Other_Dimension
FROM Part_Matrix
WHERE [Forms]![UI]![cmbFilter]
BETWEEN (((SELECT [Forms]![UI]![cmbFilter] FROM Part_Matrix WHERE Part_Number = [Forms]![UI]![lbSelected]) - [Forms]![UI]![txtTolerance])
AND ((SELECT [Forms]![UI]![cmbFilter] FROM Part_Matrix WHERE Part_Number = [Forms]![UI]![lbSelected]) + [Forms]![UI]![txtTolerance]))
ORDER BY [Forms]![UI]![cmbFilter] DESC;
I have also tried to write the SQL code in access vba still no luck, the code below was just a simple text, I know its now the same logic as above.
Private Sub btnSearch_Click()
Dim SQL As String
If txtTolerance = "" Then
MsgBox ("No Tolerance Entered")
Exit Sub
ElseIf cmbFilter = "" Then
MsgBox ("No Filter Criteria Entered")
Exit Sub
Else
SQL = "SELECT Part_Matrix.[Part_Number], " & Me.cmbFilter & " " & _
"FROM Part_Matrix" & _
"ORDER BY " & Me.cmbFilter & " DESC;"
Debug.Print SQL
DoCmd.RunSQL SQL
lbFilterResults.RowSource = SQL
lbFilterResults.Requery
End If
End Sub
Try this, using a dlookup instead of SELECT to return the values you want in the BETWEEN statement. I believe the dlookup should return the value for whatever field you select in the combo box. Also, I've simplified to remove the forms!UI statement with a "me" assuming you are running code from the same form. Let me know if this works for ya.
intTarget = dlookup(me!CmbFilter, "PartMatrix", "Part_Number = " & me!LbSelected)
intLower = intTarget - me!txtTolerance
intUpper = intTarget + me!txtTolerance
strSQL = "SELECT * FROM Part_Matrix WHERE " & me!cmbFilter & " " & _
"BETWEEN " & intLower & " AND " & intUpper
In your BETWEEN statement, reference the table's [Large_OD] field and not the form'S.
i.e
WHERE Large_OD
BETWEEN (((SELECT Large_OD FROM Part_Matrix WHERE Part_Number = [Forms]![UI]![lbSelected]) - [Forms]![UI]![txtTolerance])
AND ((SELECT Large_OD FROM Part_Matrix WHERE Part_Number = [Forms]![UI]![lbSelected]) + [Forms]![UI]![txtTolerance]))
ORDER BY " & Me.cmbFilter & " DESC;