I have an Access database, where I have a form with a textbox (named c1) and a button. When I click the button it opens a datasheet form with information filtered by the textbox value.
The button vba looks like this:
c1.Value = Replace(c1.Value, ",", " Or ")
DoCmd.OpenForm ("dsForm")
The query behind the datasheet looks something like this in design view:
Field: Name1 | Name2
Criteria: | Like [Forms]![Menu]![c1].[value]
This is so I could later export the results of this query to excel.
So my issue is that I want to enter values into the textbox and separate them with a comma, which would be later turner into an Or by vba. Why I'm doing this with 1 textbox not multiple, is because I could have many values that I want to search by.
Right now it works if I enter one value into the textbox, but when I enter 2 values it's not working. I'm pretty sure that the query is taking the whole statement as a string for example if I enter 110,220 it's supposed to be Like "110" or "220", but on the query it would be Like "110 or 220".
I've tried by setting the field to be either a string or a number as well. How would I manipulate the criteria on a query from vba?
I recommend writing a SQL string with the IN statement instead of the OR, and using the OpenArgs event to pass data from the main form over to the datasheet form.
Main Form Button Code
Dim sql as String
sql = "Select * From [table name] Where Name2 IN (" & c1 & ")"
DoCmd.OpenForm "dsForm", acFormDS, , , , , sql
Datasheet form (dsForm) Code -- Use the Form_Load event.
Private Sub Form_Load()
Me.RecordSource = Me.OpenArgs
End Sub
The IN statement allows you to use commas. The OpenArgs event allows you to pass values from one form over to another.
Actually my first method was terrible, read the values into an array like this:
Sub y()
a = "a,b,c,d"
'Split into 1d Array
b = Split(a, ",", , vbTextCompare)
For c = 0 To UBound(b)
Debug.Print b(c)
Next c
End Sub
You can loop through the array as in the debug.print loop and use each value separately.
Related
I can upload a file if someone tells me how.
Need help replicating a filter as you type on a combo box that is being used at the record level. Example: Instead of having an open text box for the prefix (e.g. Mr. Mrs. Ms. Dr.), I'm using a combo box that looks up from a reference table. I want to be able to type the letter "r" in the combo box and have it filter out Ms. and showing the remaining values. Once I make a selection store the selected value in the Name table.
Issue: When I add a new value in Combo4 the other rows above clear out if they don't match the value I just typed into the cell. Something likely with the RowSource in the below formula. Do I have something out of sequence or a flawed formula?
What I think I'm trying to do:
1) If Prefix value populated w/ value in t_Name THEN show the matching value in t_ref_Prefix
2) If Combo4 is Blank / Null THEN then open Combo4 and show all values in t_ref_Prefix so a value can be selected.
3) If user is typing text into Combo4 THEN filter on change using * on both sides of the typed value.
Option Compare Database
Option Explicit
Private Sub Combo4_Change()
'https://stackoverflow.com/questions/48133260/display-records-in-access-db-combobox-on-any-text-typed-by-user
'test number of characters entered - if greater then 0 then assign rowsource
If Len(Me.Combo4.Text) > 0 Then
'set the rowsource to match user search criteria
Me.Combo4.RowSource = "SELECT * FROM t_ref_Prefix WHERE Prefix LIKE '*" & Me.Combo4.Text & "*'"
'show the search in real-time
Me.Combo4.Dropdown
Else
'set to no
Me.Combo4.RowSource = "SELECT t_ref_Prefix.auto, t_ref_Prefix.prefix, t_ref_Prefix.sort FROM _
t_ref_Prefix ORDER BY t_ref_Prefix.sort, t_ref_Prefix.prefix"
End If
End Sub
You need to set
Combo4.AutoExpand = False
This will do it.
I am using Access 2010 database. I have a combo box that gives me a full list of equipment numbers (they have letters and numbers i.e.: sdp1234).
To try to speed up the database I was told to limit the drop down lists in combo boxes.
Currently, users can start typing the equipment # and an item from the list will be highlighted matching with their typed characters.
I want the users to be able to type "12" and the list show should have only the values between "sdp1200" and "sdp1299". Or even just all the items that have "12" inside.
I am not sure if this is done in VBA or in the properties tab for the combo box.
Well a solution would be :
Put this on the top of your VBA..just under Option ...
Dim comboboxOriginal As String
Put this code in the Change Event
Private Sub cboFilterAsType_Change()
If Len(Nz(comboboxOriginal, "")) = 0 Then
comboboxOriginal = Me.cboFilterAsType.RowSource
End If
If Len(Me.cboFilterAsType.Text) > 1 Then
Me.cboFilterAsType.SelStart = Len(Me.cboFilterAsType.Text)
Me.cboFilterAsType.RowSource = Replace(comboboxOriginal, ";", "") & " WHERE SOMEFIELD like ""*" & Me.cboFilterAsType.Text & "*"""
DoCmd.RunCommand acCmdSaveRecord
Me.cboFilterAsType.Requery
Me.cboFilterAsType.Dropdown
End If
End Sub
TO clear the filtering
Private Sub cboFilterAsType_DblClick(Cancel As Integer)
If Len(Nz(comboboxOriginal, "")) > 0 Then
Me.cboFilterAsType.RowSource = comboboxOriginal
End If
End Sub
Take a note that the RowSource should be something simple like SELECT SomeID From ATable
I spend half day to figure out how to access value in nested datasheet form record.
Please, take a look at image below.
I have dblClick event on "SID" column cell. It's field name is "txtSID".
I need to grab that value (in picture "20") and pass it in VBA SQL.
There is some trick with datasheets. Looks like they has no control name or something.
Looks like I found solution. """ & Me![txtSID] & """ does the thing.
Private Sub txtSID_DblClick(Cancel As Integer)
Dim SQL As String
SQL = "INSERT INTO documents (stakeholder_id, document_type_id, status_id) VALUES (""" & Me![txtSID] & """, 1, 1);"
DoCmd.SetWarnings False
DoCmd.RunSQL SQL
DoCmd.SetWarnings True
End Sub
Context:
In my company, some assistants fill out an Excel table, which is a users list (First Names, Last name, ID number). After, I use this list with a PowerShell script. But very often the users list is not correctly completed. For example, assistants forget to input ID number... .So i would like help assitants to fill this Excel with data suggestions/autocomplete.
Technical:
In the "Data" sheet, I have all data possible (First Names, Last name, ID number).
With the "Name Manager" I created:
d_FirstName to select the first cell
c_FirstName to select all column,
l_FirstName to apply function: =OFSSET(d_FirstName;0;0;COUNTA(c_FirstName)-1;1)
In "Form" sheet, I created drop-down list with function: =IF(A1<>"";OFSSET(d_FirstName;MATCH(A1&"*";l_FirstName;0)-1;;SUMPRODUCT((MID(l_FirstName;1;LEN(A1))=TEXT(A1;"0"))*1));l_FirstName)
So, when the user types a letter, the drop down list "suggest" a correct FirstName.
Question:
How to adapt the last query, to complete a line with First Name and Last name and ID number corresponding if user type only First Name ?
For example:
If user select a First Name in drop down list, Excel complete the lign with Last name and ID number corresponding .
If user select a ID number in drop down list, Excel complete the lign with Last name and First Name corresponding.
In second time, how to show dropdown list automatically when user type one letter ?
Thank you
You can accomplish this using the combobox's properties and change event. The combobox will take a 1 or 2 dimensional named range or a formula that returns a range as it's RowSource. Here I have the text column set to the 3rd column.
Private Sub cboEmpID_Change()
With cboEmpID
If Not IsNull(.Value) Then
lblEmployee.Caption = .List(.ListIndex, 1) & ", " & .List(.ListIndex, 0)
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim ColumnWidths As String
With Worksheets("Sheet1")
ColumnWidths = .Columns(1).Width & ";" & .Columns(2).Width & ";" & .Columns(3).Width
End With
With cboEmpID
.ColumnHeads = True
.ColumnCount = 3
.ColumnWidths = ColumnWidths
.TextColumn = 3
.ListWidth = Range("Sheet1!A:C").Width
.RowSource = "OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!$A:$A)-1,3)"
End With
End Sub
You need making a cascading dependent Excel drop down list.See
I'm setting 12 TextBox ControlSources in my Form from VBA using the following :
...
Me.Oct.ControlSource = "=DSum('GBPValue', 'MF YTD Actual Income & Adret', 'Month=10 AND Org_Type=[Key]')"
Me.Nov.ControlSource = "=DSum('GBPValue', 'MF YTD Actual Income & Adret', 'Month=11 AND Org_Type=[Key]')"
...
[Key] is the name of a textbox in the form
When the form loads up i get some odd behavior -
all of the summary form text boxes are blank as are all the dlookup text boxes
if i then click on one of the text boxes that has a dlookup control source assigned the summary text boxes for the other columns start to populate with 0's and #Num etc. and the dlookup runs and displays the expected numbers
once i've clicked on all the dlookup fields the summary numbers calc properly.
In the final version of this the query will be re-written after user clicks from the VBA so ... is this a sensible way to get the form to re-query the DB and, if so, how can i make the DLookups run/display automatically so that everything displays immediately on form load?
You are probably looking for Recalc (Me.Recalc). However, I suggest you use a recordset, rather than DlookUp, and the Current event for the form:
Dim rs As DAO.Recordset 'Needs MS DAO 3.x library
Dim db As Database
Dim strSQL As String
Set db = CurrentDb()
'Guessing that key is a form value
'Note that Month is a reserved word
strSQL = "SELECT [Month], Sum(GBPValue) As SumVal " _
& "FROM [MF YTD Actual Income & Adret] " _
& "WHERE Org_Type= " & Me.[Key]
& " GROUP BY [Month]"
Set rs=db.OpenRecordset(strSQL)
'You can probably use a Do While Loop, consider
'naming the controls, eg, Month10
rs.FindFirst "[Month]=10"
Me.Oct = rs!SumVal
'and so on