I have a form with several checkboxes in it. Values of ticking checkboxes are used after in the SQL-query including in Excel-macro. I use these values in SQL "IN"-operator. So, everythig works. But I don't like the code for my macro.
For ticking checkboxes I use such code (and if there were more value the list would be very huge):
Public Location1 As String
Public Location2 As String
Public Location3 As String
Public Location4 As String
Private Sub OKCommandButton2_Click()
If CheckBox1.Value = True Then Location1 = "LocationValue1"
If CheckBox2.Value = True Then Location2 = "LocationValue2"
If CheckBox3.Value = True Then Location3 = "LocationValue3"
If CheckBox4.Value = True Then Location4 = "LocationValue4"
...
And for using it in SQl I use such code:
query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
"WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 IN ('" & _
LocationDefinition.Location1 & "','" & LocationDefinition.Location2 & "','" & LocationDefinition.Location3 & "','" & _
LocationDefinition.Location4 & "')" & _
"ORDER BY Param2, Param3"
The question is: can I rewrite my code in more compact, laconic and sophisticated way? Maybe I should use another operator in SQL-part; maybe I can rewrite my VBA-part, for using only one parameter in SQl.
Thank you.
you can use the "controls" Feature and write your Value into the "TAG" from the checkbox
Dim TB As Control
Dim ChkBoxString As String
ChkBoxString = "("
For Each TB In Me.Controls
If TypeOf TB Is CheckBox Then
If TB.Value = True Then
ChkBoxString = ChkBoxString & TB.Tag & ", "
End If
End If
Next TB
ChkBoxString = ChkBoxString & ")"
ChkBoxString = Replace(ChkBoxString, ", )", ")")
So you can use your Script:
query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
"WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 " _
IN " & ChkBoxString
Greets Ralf
Create function that return string expression with comma delimited, and set the value in the Tag property of the checkbox
Function GetExpression() As String
Dim contr As Control
Dim comma As String
Dim str As String
str = ""
For Each contr In UserForm1.Controls
comma = IIf(str <> "", ",", "")
If TypeName(contr) = "CheckBox" Then
If contr.Value = True Then str = str + comma + contr.Tag
End If
Next
GetExpression = str
End Function
If the sub that "makes" the query (or build the query string) is called while the Userform is still loaded then you can code as follows:
Option Explicit
Dim Locations As String '<--| UserForm scoped variable: all subs in the userform code pane can "see" it
Private Sub OKCommandButton2_Click()
Dim ctrl As Control
For Each ctrl In Me.Controls '<--| loop through userform controls
If TypeName(ctrl) = "CheckBox" Then '<--| consider only checkboxes
If ctrl.value Then Locations = Locations & "LocationValue" & Mid(ctrl.Name, 9, Len(ctrl.Name) - 8) & "','" '<--| if checkbox is checked then update 'Location' string
End If
Next ctrl
End Sub
Private Sub QuerySub() '<-- name of any Sub inside Userfom code pane that has to make the query
Dim Query As String
If Locations <> "" Then '<--| this Sub can "see" 'Locations' even if it has been initialized in a different Sub of the same Userform code pane
Query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
"WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 IN ('" & _
Left(Locations, Len(Locations) - 3) & "')" & _
"ORDER BY Param2, Param3"
Else
' code to handle 'Locations' empty string
End If
End Sub
This seems a tiny bit wrong because you get IN ('', '', '', '') when unchecked, but if you don't mind that, then maybe something like this:
locations$ = Mid$( _
IIf(CheckBox1, "','LocationValue1", "") & _
IIf(CheckBox2, "','LocationValue2", "") & _
IIf(CheckBox3, "','LocationValue3", "") & _
IIf(CheckBox4, "','LocationValue4", ""), 4) ' works even is all unchecked
query = " ... AND Param2 IN ('" & locations & "') ORDER BY Param2, Param3"
or if all of the values really start with "LocationValue" then
Locations$ = Mid$(Replace(IIf(CheckBox1, ",1", "") & _
IIf(CheckBox2, ",2", "") & IIf(CheckBox3, ",3", "") & _
IIf(CheckBox4, ",4", ""), ",", "','LocationValue"), 4)
Related
This is my code below. I am trying to search a database using two different dates, company name
I am getting an error when one of the date fields is empty or null. How can I solve this issue or bypass if the date search field is empty to ignore it in the search or search for an empty field?
Dim SQL As String
SQL = "SELECT * from qryRequestInternal where ([DateRequestSent] = #" & txt_Search_Sdate & "# AND [DateReceived] = #" & txt_Search_Rdate & "# AND (companyName like ""*" & txt_SCompNa & "*"") )"
Me.sfrmRequestInternal.Form.RecordSource = SQL
Me.sfrmRequestInternal.Form.Requery
Me.sfrmRequestInternal_col.Form.RecordSource = SQL
Me.sfrmRequestInternal_col.Form.Requery
End Sub
You will need to check for Null values and build the SQL string based on the controls which have a value (i.e. not null).
The example below uses a helper function to build the sql string. If nothing is inserted, it will only run the the Select * from qryRequestInternal without any criteria.
Private Function SqlWhere() As String
Dim retvalue As String
'sent date
With txt_Search_Sdate
If Not IsNull(.Value) Then
retvalue = " WHERE [DateRequestSent] = #" & Format(.Value, "mm/dd/yyyy") & "#"
End If
End With
'received date
With txt_Search_Rdate
If Not IsNull(.Value) Then
retvalue = IIf(Len(retvalue) = 0, " WHERE", retvalue & " AND") & " [DateReceived] = #" & Format(.Value, "mm/dd/yyyy") & "#"
End If
End With
'company
With txt_SCompNa
If Not IsNull(.Value) Then
retvalue = IIf(Len(retvalue) = 0, " WHERE", retvalue & " AND") & " [companyName] Like '*" & .Value & "*'"
End If
End With
SqlWhere = retvalue
End Function
To call it:
Dim sqlString As String
sqlString = "SELECT * from qryRequestInternal" & SqlWhere()
Debug.Print sqlString
I have a form (frmSearch) that I use several (4) comboboxes to filter out results for a listbox (lstCustomers). What I'm attempting to do now is create the ability to filter the listbox based on a text box of "keywords". Additionally, the column which the keyword box will search will be variable based on cboWhere which is a list of columns from tblContacts (the table qryContactWants uses)
I found a really nice Function set with the following code that will let me filter everything, but I'm not entirely sure how to turn this data around and use it to filter out my listbox.
This function organizes the keywords:
Public Function FindAnyWord(varFindIn, strWordList As String) As Boolean
Dim var
Dim aWords
aWords = Split(strWordList, ",")
For Each var In aWords
If FindWord(varFindIn, var) Then
FindAnyWord = True
Exit Function
End If
Next var
End Function
And this function actually performs the search:
Public Function FindWord(varFindIn As Variant, varWord As Variant) As Boolean
Const PUNCLIST = """' .,?!:;(){}[]-—/"
Dim intPos As Integer
FindWord = False
If Not IsNull(varFindIn) And Not IsNull(varWord) Then
intPos = InStr(varFindIn, varWord)
' loop until no instances of sought substring found
Do While intPos > 0
' is it at start of string
If intPos = 1 Then
' is it whole string?
If Len(varFindIn) = Len(varWord) Then
FindWord = True
Exit Function
' is it followed by a space or punctuation mark?
ElseIf InStr(PUNCLIST, Mid(varFindIn, intPos + Len(varWord), 1)) > 0 Then
FindWord = True
Exit Function
End If
Else
' is it precedeed by a space or punctuation mark?
If InStr(PUNCLIST, Mid(varFindIn, intPos - 1, 1)) > 0 Then
' is it at end of string or followed by a space or punctuation mark?
If InStr(PUNCLIST, Mid(varFindIn, intPos + Len(varWord), 1)) > 0 Then
FindWord = True
Exit Function
End If
End If
End If
' remove characters up to end of first instance
' of sought substring before looping
varFindIn = Mid(varFindIn, intPos + 1)
intPos = InStr(varFindIn, varWord)
Loop
End If
End Function
And here is the code that I typically use to filter the listbox using the comboboxes on frmSearch:
Dim column As String
SQL = "SELECT qryContactWants.ID, qryContactWants.FullName, qryContactWants.Type, qryContactWants.Make, qryContactWants.Model, qryContactWants.YearWanted, qryContactWants.Condition " _
& "FROM qryContactWants " _
& "WHERE 1=1 "
If cboType.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Type = '" & cboType.Value & "'"
End If
If cboMake.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Make = '" & cboMake.Value & "'"
End If
If cboModel.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Model = '" & cboModel.Value & "'"
End If
If cboYear.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.YearWanted = '" & cboYear.Value & "'"
End If
If cboCondition.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Condition = '" & cboCondition.Value & "'"
End If
SQL = SQL & " ORDER BY qryContactWants.Last"
Me.lstCustomers.RowSource = SQL
Me.lstCustomers.Requery
End Sub
What I would like to do is take the functions I found for searching keywords and apply it to my form and aid in returning a list of customers in lstCustomers
Ideally, having the keyword function return an SQL statement similar to those I'm using to filter out the listbox would be perfect. This would allow me to add a simple SQL = SQL & "AND qryContactWants.VARIABLECOLUMNHERE =SOMETHING
EDIT 1:
While using the following code, VBA is tossing a compile error on the second "End If" stating there isn't a Block If. There clearly is, so I'm not sure what's going on. Here is the code I'm using:
Public Function KeyWhere(strKeys As String, strColumn As String) As String
Dim b As Variant
strKeys = Replace(strKeys, vbCrLf, ",") ' remove all line returns
b = Split(strKeys, ",")
Dim strWhere As String
Dim v As Variant
For Each v In b
If Trim(b) <> "" Then
If strWhere <> "" Then strWhere = strWhere & " or "
strWhere = strWhere & strColumn & " like '*" & Trim(v) & "*'"
End If
End If
Next
strWhere = "(" & strWhere & ")"
KeyWhere = strWhere
End Function
And under the function RequerylistCustomers() I added the If IsNull (Me.txtSearch) = False Then code below:
Private Sub RequerylstCustomers()
Dim SQL As String
'Dim criteria As String
Dim column As String
SQL = "SELECT qryContactWants.ID, qryContactWants.FullName, qryContactWants.Type, qryContactWants.Make, qryContactWants.Model, qryContactWants.YearWanted, qryContactWants.Condition " _
& "FROM qryContactWants " _
& "WHERE 1=1 "
If cboType.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Type = '" & cboType.Value & "'"
End If
If cboMake.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Make = '" & cboMake.Value & "'"
End If
If cboModel.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Model = '" & cboModel.Value & "'"
End If
If cboYear.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.YearWanted = '" & cboYear.Value & "'"
End If
If cboCondition.Value & "" <> "" Then
SQL = SQL & " AND qryContactWants.Condition = '" & cboCondition.Value & "'"
End If
Dim strWhere As String
'Grab Keywords from txtSearch using cboWhere to search for those keywords
If IsNull(Me.txtSearch) = False Then
strWhere = KeyWhere(Me.txtSearch, Me.cboWhere)
SQL = SQL & " AND " & strWhere
End If
SQL = SQL & " ORDER BY qryContactWants.Last"
Me.lstCustomers.RowSource = SQL
Me.lstCustomers.Requery
End Sub
Are the keywords to be searched in a single column (say a comments or memo column?). If yes, then you should be able to optional "add" the one additional criteria to your current "set" of combo box filters.
Are we to assume that the keywords can appear anywhere in that memo column to search?
So, if there are "key words entered into that text box, then you call KeyWhere.
eg this routine:
Public Function KeyWhere(strKeys As String, strColumn As String) As String
Dim b As Variant
strKeys = Replace(strKeys, vbCrLf, ",") ' remove all line returns
b = Split(strKeys, ",")
Dim strWhere As String
Dim v As Variant
For Each v In b
if trim(v) <> "" then
If strWhere <> "" Then strWhere = strWhere & " or "
strWhere = strWhere & strColumn & " like '*" & Trim(v) & "*'"
end if
Next
strWhere = "(" & strWhere & ")"
KeyWhere = strWhere
End Function
We assume each key word is separated by a comma (could be space, but comma is better).
So, if I type in the following command in debug window to test the above?
? keywhere("Generator, Water maker, Battery","Notes")
OutPut:
(Notes like '*Generator*' or Notes like '*Water maker*' or Notes like '*Battery*')
So, we just append the above results to your final SQL.
eg:
dim strWhere as string
if isnull(me.KeyWordBox) = False then
strWhere = keyWhere(me.KeyWordBox,me.cboColumnToSearch)
SQL = SQL & " AND " & strWhere
end if
so, the above converts all keywords into a valid SQL condition for the column to search. It is likely that column is some kind of notes column, but it would work for other description type field to search.
I use the following code in vba access to update a column of a table, but it is not working. Please help me.
Best regards.
Dim sqlupdate As String
sqlupdate = "UPDATE Assay" _
& "SET Assay.assay_repeat = " & 0 & "" _
& "WHERE (((Assay.[assay_repeat])= " & 1 & "));"
DoCmd.RunSQL sqlupdate
You have an extra double quote and are missing a couple of spaces - try it like this:
Dim sqlupdate As String
sqlupdate = "UPDATE Assay" _
& " SET Assay.assay_repeat = " & 0 & " _
& " WHERE (((Assay.[assay_repeat])= " & 1 & "));"
You just missed space chars at end of the table name and before where.
Dim sqlupdate As String
sqlupdate = "UPDATE Assay " _
& "SET Assay.assay_repeat = " & 0 & " " _
& "WHERE (((Assay.[assay_repeat])= " & 1 & "));"
Here is a great way to convert a SQL string to VBA code.
Creating the form
The form just needs two text boxes, and a command button. SQL statements can be quite long, so you put the text boxes on different pages of a tab control.
Create a new form (in design view.)
Add a tab control.
In the first page of the tab control, add a unbound text box.
Set its Name property to txtSql.
Increase its Height and Width so you can see many long lines at once.
In the second page of the tab control, add another unbound text box.
Name it txtVBA, and increase its height and width.
Above the tab control, add a command button.
Name it cmdSql2Vba.
Set its On Click property to [Event Procedure].
Click the Build button (...) beside this property.
When Access opens the code window, set up the code like this:
Private Sub cmdSql2Vba_Click()
Dim strSql As String
'Purpose: Convert a SQL statement into a string to paste into VBA code.
Const strcLineEnd = " "" & vbCrLf & _" & vbCrLf & """"
If IsNull(Me.txtSQL) Then
Beep
Else
strSql = Me.txtSQL
strSql = Replace(strSql, """", """""") 'Double up any quotes.
strSql = Replace(strSql, vbCrLf, strcLineEnd)
strSql = "strSql = """ & strSql & """"
Me.txtVBA = strSql
Me.txtVBA.SetFocus
RunCommand acCmdCopy
End If
End Sub
http://allenbrowne.com/ser-71.html
I recommend you use Recordsets.
Private Sub Update_My_Records(Parent As Object)
Dim Data_Recset As Object
Dim Parent_Reference As Object
Set Data_Recset = Parent_Reference.Application.DBEngine.Workspaces(0).Databases(0).OpenRecordset("SELECT * FROM Assay WHERE assay_repeat = " & 0 & ";", DB_OPEN_DYNASET)
Data_Recset.MoveLast
Data_Recset.MoveFirst
Data_Recset.Edit
Data_Recset.Fields("assay_repeat") = 1
Data_Recset.Update
Data_Recset.Close
Set Data_Recset = Nothing
End Sub
assumptions
Parent has reference to Access.Application. (I usually pass: Form.Module.Parent reference to Sub/Function)
the table or query "Assay" already exists.
You only need to update 1 row at a time
But if you want to use Queries In Your Form:
Private Sub Query_Definition_Update()
Dim Obj_Qdef As Object
Dim Query_Name As String
Query_Name = "Q_Assay"
Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs.Refresh
Set Obj_Qdef = Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs(Query_Name)
Obj_Qdef.SQL = Query_Update(1)
Debug.Print Obj_Qdef.SQL
Obj_Qdef.Execute
''When finished updating
Obj_Qdef.Close
Set Obj_Qdef = Nothing
End Sub
'------------------------------------------------------------'
Private Function Query_Update(New_Value as Integer) As String
Query_Update = "UPDATE Assay" & _
" SET Assay.assay_repeat = " & 0 & "" & _
" WHERE (((Assay.[assay_repeat])= " & New_Value & "));"
End Sub
I've never really used Farpoint Spread before, but I have an existing VB.NET application in which I need to add a column to a Spread grid. There is currently some code like:
For Each dr As DataRow In g_AdoRS.Rows
vaSpreadSum.SetText(1, x, dr(0)) 'pol_ser
...
vaSpreadSum.SetText(20, x, dr(19)) 'renew_pay_cd
vaSpreadSum.SetFloat(21, x, dr(20)) 'renew_tot_prem
vaSpreadSum.SetFloat(22, x, dr(21)) 'renew_pol_limit
vaSpreadSum.SetFloat(23, x, dr(22)) 'renew_ded_amt
vaSpreadSum.Col = 28
x = x + 1
Next dr
These SetFloat() and SetText() calls go from 0 to 28. So in order to add another column I added this line of code:
vaSpreadSum.SetText(28, x, dr(27)) 'agent name
and changed the vaSpreadSum.Col to 29
vaSpreadSum.Col = 29
But I am not seeing another column in my grid. Any idea why? There is no error thrown or anything like that, just no changes on the screen. I know there is probably more information needed to solve this, but even if anyone know the basics of adding a column to a Farpoint Spread grid that would be much appreciated. I found this but it doesn't seem that my application is adding columns that way, I couldn't find any calls to the AddColumns() method anywhere.
Thanks for any help!
I believe this is my Form_Load method
Private Sub FrmDetailRPC_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Cursor.Current = Cursors.WaitCursor
FormInit()
QryLocation()
Cursor.Current = Cursors.Default
End Sub
I'll also include FormInit() because that sounds like it might have to do with what I'm looking for
Sub FormInit()
txtBusUnit.Text = svBusUnit
stmtMktSeg()
txtProduct.Text = svProduct
txtSource.Text = svSource
txtSystem.Text = svSystem
txtCustSeg.Text = svCustSeg
stmtProduct()
txtLocation.Text = svLocation
If svLocationLabel = "Region" Then
lblLocation.Text = "Territory"
Else
lblLocation.Text = svLocationLabel
End If
lblLocation.TextAlign = ContentAlignment.TopRight
stmtLocation()
'txtPayType.Text = svPayType
txtTimePer.Text = TimeName
stmtTimePer()
End Sub
And QryLocation()
Sub QryLocation()
Dim producerID As String
'SetProductSelection()
stmtLocation()
stmtGetProductType()
stmtGetTimePeriodType()
stmtGetTimePeriod()
stmtGetProducerID()
stmtGetProducerType()
If stmtProducerType = "No Preference" Then
producerID = "NULL"
Else
producerID = "'" & stmtProducerID & "'"
End If
g_strSQL = "pc_mis_rpc_getdata_detail " & _
"'" & stmtLocationType & "'," & _
"'" & Trim(svLocation) & "'," & _
"'" & svBusUnit & "'," & _
"'" & stmtProductType & "'," & _
"'" & Trim(stmtProductDtl) & "'," & _
"'" & stmtTimePeriod & "'," & _
"'" & stmtTimePeriodType & "'," & _
"'" & stmtProducerType & "'," & _
producerID & "," & _
"'Retention'" _
& FilterQry & "," & _
"'" & Trim(txtCustSeg.Text) & "'," & _
"'" & Trim(txtSource.Text) & "'," & _
"'" & Trim(txtSystem.Text) & "'"
ProcQry()
End Sub
In your Form_Init() you will need to adjust the number of columns in spread control.
It should look something like this:
Sub FormInit()
' Add a column to the spreadsheet control
vaSpreadSum.ActiveSheet.AddColumns(29, 1)
' Code cut for brevity
End Sub
--or--
Sub FormInit()
' Add a column to the spreadsheet control
vaSpreadSum.ActiveSheet.Columns.Count = 29
' Code cut for brevity
End Sub
Another method for achieving the same thing is to open the form designer, select the spread control, display the properties window (press F4 if it isn't already open), and increase the Cols property to 29.
You increase the column as follows:
vaSpreadSum.MaxCols = 29
My goal is to return the results of 5 textboxes into a SQL query, by incorporating the Query string with the variables.
How can I get my code to function so that when a checkbox is checked, the value (eg: ID, SC...) is recorded and placed into a Query? And if a checkbox is not checked, then it is not placed into the query.
The 5 checkboxes are as follows:
The code I current have to record whether a textbox is selected, and to place the value (eg: ID, SC, AS...) into a variable is as follows:
If (Me.BoxID = False) And (Me.BoxSC = False) And (Me.BoxASSC = False) And (Me.BoxAS = False) And (Me.BoxEH = False) Then
MsgBox "You must check a Fonction Checkbox", 0
Exit Sub
Else
If (Me.BoxID= True) Then IDValue = Chr(34) & "ID" & Chr(34) Else IDValue = """"""
If (Me.BoxSC= True) Then SCValue = Chr(34) & "SC" & Chr(34) Else SCValue = """"""
If (Me.BoxASSC= True) Then ASSCValue = Chr(34) & "ASSC" & Chr(34) Else ASSCValue = """"""
If (Me.BoxAS= True) Then ASValue = Chr(34) & "AS" & Chr(34) Else ASValue = """"""
If (Me.BoxEH= True) Then EHValue = Chr(34) & "EH" & Chr(34) Else EHValue = """"""
End If
fonctionQryString = "(((tblF.f1)=" & IDValue & ") OR " + "((tblF.f1)=" & SCValue & ") OR " + "((tblF.f1)=" & ASSCValue & ") OR " + "(tblF.f1)=" & ASValue & ") OR " + "(tblF.f1)=" & EHValue & ")))"
The fonctionQryString goes into the WHERE section of the SQL Query.
I know that the method I'm using is not efficient, even though it works.
My problem is that I don't know how to do this another way. I want my code to function so that when a checkbox is not checked, it doesn't go into the Query string.
Any help would be much appreciated.
These two WHERE clauses should produce equivalent results. Consider switching to the second form.
WHERE tblF.f1 = "ID" OR tblF.f1 = "SC" OR tblF.f1 = "AS"
WHERE tblF.f1 IN ("ID","SC","AS")
Here is a rough and untested code sample to produce a similar WHERE clause based on my understanding of what you're trying to achieve.
Dim fonctionQryString As String
Dim lngLoopNum As Long
Dim strControlName As String
Dim strValueList As String
For lngLoopNum = 1 To 5
Select Case lngLoopNum
Case 1
strControlName = "ID"
Case 2
strControlName = "SC"
Case 3
strControlName = "ASSC"
Case 4
strControlName = "AS"
Case 5
strControlName = "EH"
End Select
If Me.Controls("Box" & strControlName) = True Then
strValueList = strValueList & "," & Chr(34) & _
strControlName & Chr(34)
End If
Next
If Len(strValueList) > 0 Then
fonctionQryString = "tblF.f1 IN (" & Mid(strValueList, 2) & ")"
Else
MsgBox "You must check a Fonction Checkbox"
End If
I assumed you didn't actually want to include the condition, WHERE tblF.f1 = "" (an empty string). If I guessed wrong, you'll have more work to do, but hopefully this will still point you to something useful.