Access 2013 - Dlookup with double - vba

Hello i've a little problem with my VBA code i'm trying to select the right gps number (double) which matches to the string Name in table tblpersonal and the string in the textbox tabletbesitzerbox. The GPS number should be displayed in the textbox fkgps:
Private Sub SP_Besitzersuche_Click()
DoCmd.OpenForm "F-Tablet-Hinzufuegen-Neu"
Dim Sim As Double
Sim = Nz(DLookup("[GPS]", _
"tblPersonal", _
"Name = " & Forms![F-Tablet-Hinzufuegen-Neu]![tabletbesitzerbox]), "")
FKGPS.Value = Sim
End Sub
The error shows me: Syntaxerror (missing Operation) in query expression 'Name = XY'
I'm thankful for every help :)

String parameters must be enclosed in quotes. When building the criteria in VBA, it is easiest to use single quotes:
Sim = Nz(DLookup("[GPS]", _
"tblPersonal", _
"Name = '" & Forms![F-Tablet-Hinzufuegen-Neu]![tabletbesitzerbox] & "'"), 0)

Related

Filter subform by using combo box

I have a combo box on a form that I want to filter a subform (SubSearchMaster_frm).
I am receiving:
Runtime error 3464: data type mismatch in expression.
The code is below:
Private Sub CboNIIN_AfterUpdate()
Me.SubSearchMaster_frm.Form.Filter = "[NIIN] = " & Me.CboNIIN
Me.SubSearchMaster_frm.Form.FilterOn = True
End Sub
The subform is a query.
I have also tried:
Private Sub CboNIIN_AfterUpdate()
Dim sql As String
sql = "Select * from SubSearchMaster_frm where ([NIIN] = " & Me.CboNIIN & ") From subsearchmaster_frm"
Me.SubSearchMaster_frm.Form.RecordSource = sql
Me.SubSearchMaster_frm.Form.Requery
End Sub
But I'm getting an error on that too.
Since you have stated that the NIIN field is of Text datatype, you will need to surround the filter value with single or double quotes else you will receive the familiar datatype mismatch error message.
For example:
Me.SubSearchMaster_frm.Form.Filter = "[NIIN] = '" & Me.CboNIIN & "'"
Without the quotes, a numerical value is being supplied, thus resulting in a data type mismatch.

MS-ACCESS VBA Multiple Search Criteria

In my GUI, I have several ways to filter a database. Due to my lack of knowledge, my VBA programming has exploded with nested IF statements. I am getting better at using ACCESS now, and would like to find a more succinct way to perform multiple filters. My form is continuous.
Is there a simple way to do the following task (I made a toy model example):
I have a combo box SITE where I can filter by work sites A, B, C. After filtering by SITE, I have three check boxes where the user can then filter by item number 1-10, 11-20, 21-30, depending on what the user selects.
Is there a way to append multiple filters (or filter filtered data)? For example, filter by SITE A, then filter A by item number 1-10?
Currently, for EACH check box, I then have an IF statement for each site. Which I then use Form.Filter = . . . And . . . and Form.FilterOn = True.
Can I utilize SQL on the property sheet to filter as opposed to using the VBA?
What I do for these types of filters is to construct a SQL statement whenever one of the filter controls is changed. All of them reference the same subroutine to save on code duplication.
What you do with this SQL statement depends on what you're trying to do. Access is pretty versatile with it; use it as a RecordSource, straight execute it, and use the results for something else, even just printing it to a label.
To try to modularize the process, here's an example of how I do it:
Dim str As String
str = "SELECT * FROM " & Me.cListBoxRowSource
Me.Field1.SetFocus
If Me.Field1.Text <> "" Then
str = AppendNextFilter(str)
str = str & " SQLField1 LIKE '*" & Me.Field1.Text & "*'"
End If
Me.Field2.SetFocus
If Me.Field2.Text <> "" Then
str = AppendNextFilter(str)
str = str & " SQLField2 LIKE '*" & Me.Field2.Text & "*'"
End If
Me.Field3.SetFocus
If Me.Field3.Text <> "" Then
str = AppendNextFilter(str)
str = str & " SQLField3 LIKE '*" & Me.Field3.Text & "*'"
End If
Me.cListBox.RowSource = str
Variables edited to protect the guilty.
My AppendNextFilter method just checks to see if WHERE exists in the SQL statement already. If it does, append AND. Otherwise, append WHERE.
Making quite a few assumptions (since you left out a lot of info in your question), you can do something like this:
Dim sSql as String
sSql = "Select * from MyTable"
Set W = Me.cboSite.Value
sSql = sSql & " WHERE MySite = " & W & ""
Set X = Me.Chk1
Set Y = Me.Chk2
Set Z = Me.Chk3
If X = True Then
sSql = sSql & " And MyItem between 1 and 10"
If Y = True Then
sSql = sSql & " And MyItem between 11 and 20"
If Z = True Then
sSql = sSql & " And MyItem between 21 and 30"
End If
DoCmd.ExecuteSQL sSql
Again, this is entirely "air code", unchecked and probably needing some edits as I haven't touched Access in some time and my VBA is likely rusty. But it should put you on the right track.
The way i use combobox filtering in access is first I design a Query that contains all the data to be filtered. The Query must contain fields to be used for filtering. QueryAllData => "SELECT Table.Site, Table.ItemNumber, FROM Table;" Then make a copy of the query and Name it QueryFilteredData and Design the report to display the data using QueryFilteredData.
Then create a form with a Site ComboBox, ItemNumber Combo Box, and Sub Report Object and Assign SourceObject the Report Name. Use Value List as the combo box Row Source type and type in the values for Row Source to get it working. To get the report to update I always unassign the SubReport.SourceOject update the QueryFilteredData and then Reassign the SubReport.SourceObject
Combobox_Site_AfterUpdate()
Combobox_ItemNumber_AfterUpdate
End Sub
Combobox_ItemNumber_AfterUpdate()
Select Case Combobox_ItemNumber.value
Case Is = "1-10"
Store_Filters 1,10
Case Is = "11-20"
Store_Filters 11,20
Case Is = "21-30"
Store_Filters 21,30
Case Else
Store_Filters 1,10
End Sub
Private Sub Store_Filters(Lowest as integer, Highest as integer)
Dim SRpt_Recset As Object
Dim Temp_Query As Variant
Dim Temp_SourceObject as Variant
Temp_SourceObject = SubReport.SourceObject
SubReport.SourceObject =""
Set SRpt_Recset = CurrentDb.QueryDefs("QueryFilteredData")
Filter_Combo_Box1 = " ((QueryAllData.[Sites])= " & Chr(39) & Combo_Box1 & Chr(39) & ") "
Filter_Combo_Box2 = (Filter_Combo_Box1 AND (QueryAllData.ItemNumber <= Highest)) OR (Filter_Combo_Box1 AND (QueryAllData.ItemNumber >= Lowest));"
Temp_Query = " SELECT " & Query_Name & ".* " & _
"FROM " & Query_Name & " " & _
"WHERE (" & Filter_Combo_Box2 & ") ORDER BY [Field_Name_For_Sorting];"
SRpt_Recset.SQL = Temp_Query
'Debug.print Temp_Query
SubReport.SourceObject = Temp_SourceObject
End Sub
After the Combo Boxes Work if the Data is going to Change like Site and Item Number then you might want to change the Row Source of the combo boxes to Use a Query that uses Select Distinct Site From QueryAllData. I don't know if Filter_Combo_Box2 step so it may need some correction. Hope this helps.

How to write a numeric field in a VBA expression using MS Access 2016?

I have a code line referencing a field as a Date format. I need to reference another field as numeric format.
This is my code for a date format field:
Private Sub cmbsearch_Click()
Dim varFilter As Variant, stdcard_id As String, txtname As String, txtrecord
As String, strTableName As String
strTableName = "tbl01"
If IsNull(Me.stdcard_id) Then
Me.stdcard_id.SetFocus: Exit Sub
End If
varFilter = "[dateofbirth]= # "& Forms!frm_stid_name_record & "#"
....
End Sub
This is my code line for a numeric format field, but I don't know to write the code after the "=" symbol
varFilter = "[std_id]="
try
Dim stdid As Long
stdid = Val(Forms!frm_stid_name_record)
varFilter = "[std_id]=" & stdid & " "

How can I convert a double to a Hex string?

I'm working on editing a project in visual basic, and don't have a whole lot of experience with vb.
I have a text box where a user can enter a number. The number should be stored as a double. Then, I need to convert the number to it's equivalent 16-byte hexadecimal representation. Any tips on how to do this?
I have just had this kind of problem. My idea was to take the time from the function Now() and to split it by the comma. Then to convert it to Hex. After some time of researching, I came up with the idea that the input should be rounded. to 8, otherwise we may get an overflow error. This is my code:
Public Function codify_time() As String
If [set_in_production] Then On Error GoTo codify_Error
Dim dbl_01 As Variant
Dim dbl_02 As Variant
Dim dbl_now As Double
dbl_now = Round(Now(), 8)
dbl_01 = Split(CStr(dbl_now), ",")(0)
dbl_02 = Split(CStr(dbl_now), ",")(1)
codify_time = Hex(dbl_01) & "_" & Hex(dbl_02)
On Error GoTo 0
Exit Function
codify_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure codify of Function TDD_Export"
End Function

VBA ACCESS Comparing String as they are integer

I am trying to prompt the user to input a range and display all the instruments that are within that range in a subform.
Problem: The upper and lower range is a text field (because some of the range cannot be expressed in integer). As seen in the screenshot, the comparison only compare the first character of the field.
User's input: 5 - 3
On the subform: 36 - 4
It compares 5 and 3 instead of 36
I know vba is doing what it has been told but how can I achieve the result I want?
Here is my code for requering the subform:
Dim Up As Integer
Dim Low As Integer
If Me.Text_L = "" Or IsNull(Me.Text_L) Or Me.Text_U = "" Or IsNull(Me.Text_U) Then
MsgBox ("Please choose a valid range!")
Else
Up = Me.Text_U
Low = Me.Text_L
SQL = SQL_Origin & " WHERE [qry_View_Search].[Upper_Range] <= '" & Up & "' " _
& "AND [qry_View_Search].[Lower_Range] >= '" & Low & "';"
subform_View_Search.Form.RecordSource = SQL
subform_View_Search.Form.Requery
End If
so what i did is made a new column in the query for
IIf(IsNumeric([Upper]), Val([Upper]), Null)
to get all the numeric result.
Then in the vba, I re query the subform as below
SQL = SQL_Origin & " WHERE [qry_View_Search].[Upper] <= cint(Forms![frm_View_Search]![Text_U]) " _
& "AND [qry_View_Search].[Lower] >= cint(Forms![frm_View_Search]![Text_L]);"
Thanks #HansUp !
I have successfully for those cases used Val only:
Value: Val([FieldName])
or:
Value: Val(Nz([FieldName]))