Where statement not working - sql

I have read and read posts that seem to answer my exact question, but nothing I try works. I'm clearly missing something simple. I have a report showing employee overtime. I want the user to be able to click a hyperlink or button for the overtime entry and open a form to edit the entry.
The following code pulls up a parameter input box for Overtime_ID. Also entering the Overtime_ID manually will open the correct form, but it seems to default to the first record in the underlying table.
Private Sub Overtime_ID_Click()
Dim recordID As Integer
recordID = Me.Overtime_ID
DoCmd.OpenForm "frmEditOvertime",,, "Overtime_ID = " & recordID, acFormEdit
End Sub
Record Source for frmEnterOvertime is a table - Overtime Awards.
The columns in that table are:
Overtime ID (AutoNumber and PK)
Entry Date
Overtime Date
Squad List (short text)
Overtime Hours(number)
Supervisor(number)
Comment(short text)
Mandatory (Yes/No)
Correction (Yes/No)
Officer ID(Number)

Because Overtime ID has a space in its name, simply bracket [] or use backticks in OpenForm call. You cannot simply use underscore as replacement as that changes the column identifier.
DoCmd.OpenForm "frmEditOvertime",,, "[Overtime ID] = " & recordID, acFormEdit
DoCmd.OpenForm "frmEditOvertime",,, "`Overtime ID` = " & recordID, acFormEdit
And do note the where argument can use any compliant SQL WHERE condition.

Related

Find data location based on Primarykey ID

I am very new to access and vba so apologies in advance for glaring conceptual gaps.
Basically I have a button that, when clicked, prompts a user to enter an ID number. The ID entered will correspond to one of my primary keys in a specific table (I have some checks in place to make sure the entered ID is valid).
I need my code to identify the row number of the ID entered and be able to populate a specific column in that row. I know Access does not refer to data locations using rows/cols but that is the best way I thought to describe it.
I am using Index/Seek/NoMatch to make sure the entered ID number is valid and I have been trying to find a way to also use those functions to generate the data location but have not had any luck. Any help would be greatly appreciated.
I think DLookup() function will work for your case. Suppose you have following dataset in table tblEmpInfo where EmpID is primary key field.
If you want to return employee name by entering employee id in a inputbox then use DLookup() like below-
Private Sub cmdDataLookup_Click()
Dim strEmpName As String, InputID As String
InputID = InputBox("Enter employee ID:", "Input ID")
strEmpName = DLookup("EmpName", "tblEmpInfo", "EmpID= '" & InputID & "'")
MsgBox strEmpName
End Sub
If you want to update any name by entering EmpID then use UPDATE SQL statement. Try below.
Private Sub cmdUpdateInfo_Click()
Dim strSQL As String, InputID As String
InputID = InputBox("Enter employee ID:", "Input ID")
strSQL = "UPDATE tblEmpInfo Set EmpName= 'Updated Value' WHERE EmpID= '" & InputID & "'"
CurrentDb.Execute strSQL
End Sub
Note: There is no direct way to get row number and column number of a Field in access but you can apply some trick by VBA to get row and column number of a Field. I will suggest to ask a separate question to get that.

Access VBA Run-time error 3075 syntax error (missing operator) open on double click from listbox

My goal is to open a specific record from a list box to populate a different form with that specific data that was double clicked on. My column name is Claim ID 15 on the list box and in the master table that contains all data. This column contains values such as C123456789. On the Master table, Claim ID 15's data type is short text. When I double click on the row in the list box it gives me the following error: Run-time error '3075': syntax error (missing operator)in query expression '[Claim_ID_15=C123456789'.
Private Sub SearchList_DblClick(Cancel As Integer)
DoCmd.OpenForm "profileForm", , , "Claim ID 15=" & SearchList
End Sub
I have a rudimentary understanding of visual basic, thank you in advance.
Advise not to use spaces nor punctuation/special characters in naming convention. If you do, must enclose object names in [ ] characters. Also, parameters for text fields must have apostrophe delimiters. Use # for date/time field, nothing for number field.
DoCmd.OpenForm "profileForm", , , "[Claim ID 15]='" & SearchList & "'"
Thank you for the help and the information! I made the changes and it worked
Private Sub SearchList_DblClick(Cancel As Integer)
DoCmd.OpenForm "profileForm", , , "[Claim ID 15]='" & SearchList & "'"
End Sub

How can I use variable interactions in vb.net?

I am trying to create a new app that uses a SQL database to pull out data based on user input/selections.
For this I am using:
1 combo box that displays the projects
1 combo box that displays the available columns to be used while querying the table
1 text box in which the user can input data to complete the query requirements
Also I am using params to query the database as I found out this is the right way to do it (to avoid any unwanted SQL injection attacks).
So far the sub looks like this:
Private Sub FindItem()
SQL.AddParam("#item", "%" & TxtItem.Text & "%")
SQL.AddParam("#project", "%" & cbxproj.Text & "%")
SQL.AddParam("#column", "%" & cbxcol.Text & "%")
loadgrid("Select
SourceServer
,ProjectId
,ProjectName
,MasterTitle
,BugIdPrefix
,BugId
,BugTitle
,BugDescription
,EpicName
,Submitter
,AssignedByPerson
,IssueType
,Priority
,Severity
,PlatformFound
,ProgressStatus
,Resolution
,CloseReason
,Feature
,SubFeature
,FoundVersion
,AffectedVersion
,FixVersion
,GameArea
,BuildFound
,BuildFixed
,ClFixed
,Component
,Label
,CurrentOwner
,TestType
,TimeEstimated
,TimeRemaining
,TimeLogged
,Studio
,TestTeam
,ScrumTeam
,createdtime_utc as 'Date Created'
,assignedtime_utc as 'Date Assigned'
,closedtime_utc as 'Date Closed'
,lastmodifiedtime_utc as 'Date Last Modified'
From mydatabase where ProjectName like #project and #column like #item")
End Sub
So the issue is that if I select a project and a column and then press the search button, the results are displayed. Once I input some text in the text box, there are no results displayed whatsoever.
What am I doing wrong here?

Access VBA not working to find entries in database between a date range

Background
I have a form that have multiple button on the page. Each button is used to create a html report so that the data can be easily emailed to other people. I have managed to get 2 button working fine and am about 90% the way to getting the other 2 works as they use the same code, just pass different variables in to the sub.
Problem
One of the buttons is to select all records from the "Questions" table where the "CheckDate" field is between 2 dates selected on the form. This is where I am falling down and don't seem to be able to get any search results at all. I have tried using plain text as a search string so that I know the statement is correct but this also doesn't work.
Data
Table = Questions
Fields (Type):
ID (AutoNumber)
NameID (Number)
CheckDate (Date/Time)
Problem (Number)
LogScreen (Yes/No)
ActionPlan (Yes/No)
Owner (Yes/No)
DueDate (Yes/No)
LatestStatus (Yes/No)
KE (Yes/No)
Status (Yes/No)
Links (Yes/No)
RootCause (Yes/No)
Monitoring (Yes/No)
Code
Below are the relative sections of my code (I haven't put it all in as there is a lot that is not relative):
strSQL = "SELECT * FROM Questions WHERE CheckDate BETWEEN #" & strFrom & "# AND #" & strTo & "#"
Set rstQuestions = dbsQualityCheck.OpenRecordset(strSQL, dbOpenDynaset)
If Not (rstQuestions.EOF And rstQuestions.BOF) Then
rstQuestions.MoveFirst
Do Until rstQuestions.EOF = True
REST OF CODE
Loop
Else
MsgBox "There are no checks against this Analyst"
End If
Issue
The issue here is that I always end up with the Message Box advising that there are no checks against the analyst. I can only assume that the data is not correct or the date is not formatted correct on one side of the check but I am unable to see where the fault lies.
I have also tried the following as the strSQL string:
strSQL = "SELECT * FROM Questions WHERE CheckDate BETWEEN #29/11/2015# AND #01/12/2015#"
try to format your criteria as
SELECT * FROM Questions WHERE
CheckDate BETWEEN #"
& format(strFrom,"yyyy/mm/dd") &
"# AND #"
& format(strTo,"yyyy/mm/dd") & "#"

Display second last record in TextBox (show user when he last Login)

When ever user Sign In into access (achieved through SQL in VBA) following info is captured in LoginAuditTbl
LoginAuditID - Primary Key, Autonumber
LoginTime - Date/Time Field
StaffID - Number which is Foreign key to StaffTbl
LoginSuccess- A Yes/No Field
I have a textBox, where I want to display when user last sign in.
For this I will need to filter the table on StaffID, for this I have stored which User is signed in value in [TempVars]![currentUserID]
I tried this code in Default Value Property.
DLast("LoginTime","LoginAuditTbl","LoginBy = " & [TempVars]![currentUserID] & " and LoginSuccess = " & "TRUE")
But this gives Last Record. Which is the Current Sign in.
Any Function to get last record?
Or, any technique to set TextBox.Value in VBA? on form load event?
I came across Get 2nd last record in mySQL, but this will give me a record, how will I assign the value to TextBox.Value Field
First, create a query to get the latest date per user:
SELECT StaffID, Max(LoginTime) AS MaxLoginTime
FROM LoginAuditTbl
WHERE LoginSuccess = True
GROUP BY StaffID;
Second, use a DLookUp in your textbox to get the value from the query:
=DLookUp("MaxLoginTime","qryMaxLoginTimes","StaffID=" & [TempVars]![currentUserID])