Docmd.Openform Opening a blank Form? - ms-access-2007

I have a small piece of code that is embedded into a button on a continuous form. It will open a form to give additional details not on the search result.
My problem is this, It seems that when all the details are present the form opens correctly, if any fields are missing (say a pay date or voucher number), the form opens blank?
Here is the code for the search query, and the code to the button to open the form:
Private Sub Command1_Click()
Dim strsearch As String
Dim strText As String
strText = Me.txtSearch.Value
strsearch = "Select * from tblInvoiceLog where Vendor_Number like ""*" & strText & "*"" or Vendor_Name like ""*" & strText & "*"" or Invoice_1 like ""*" & strText & "*"" or Invoice_2 like ""*" & strText & "*"" or Invoice_3 like ""*" & strText & "*"" or Invoice_4 like ""*" & strText & "*"" or Invoice_5 like ""*" & strText & "*"" or Check_Request_Total like ""*" & strText & "*"" or Voucher_Number like ""*" & strText & "*"" or Notes like ""*" & strText & "*"" or TransAction_Id like ""*" & strText & "*"" ORDER BY [Pay_Date] DESC"
Me.RecordSource = strsearch
End Sub
Private Sub Command53_Click()
DoCmd.OpenForm "frm: Check Request Info (Redesign)", , , "TransAction_Id = " & TransAction_ID
End Sub
I thought I accounted for all the variables in this equation?
Can any one offer a suggestion?

Make sure that your record sources all point to the same table/query when using multiple forms/sub forms. A novice answer/problem for sure, but it could save a headache Thank you Wayne G. Dunn

Related

Access VBA for checkboxes

I'm currently working on a project that gave me a lot of raw data I am trying to filter in a continuous form. I have one field that has a possibility of five letters (GRUBW) with each multiple letters per entry. The goal is to filter the form with a checkbox for each letter. Am I missing something or going about this the wrong way?
Below is the coding I'm attempting to use and when trying to debug I keep getting an error of an undefined variable. As you can see in the coding, I have a lot of ways I want to filter. I have a feeling that I'm missing some double quotes somewhere or my understanding of the way checkboxes are to be coded is totally wrong. I've only been working with VBA for a few months, so I may be out of my depth here. Thanks.
Option Compare Database
Option Explicit
Private Sub CBTN_CLICK()
Dim ctl As Control
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acCheckBox
ctl.Value = Null
End Select
Next
Me.FilterOn = False
End Sub
Private Sub requeryform()
Dim strwhere As String
Dim lnglen As Long
If Not IsNull([Forms]![sbx]!CTB) Then
strwhere = strwhere & "([CARD] LIKE ""*" & [Forms]![sbx]!CTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!OTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!ARTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!TTB) Then
strwhere = strwhere & "([ctype] LIKE ""*" & [Forms]![sbx]!TTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!CTTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Me.RC = True Then
strwhere = strwhere & "([color] LIKE ""*" & R & "*"") AND "
End If
If Me.UC = True Then
strwhere = strwhere & "([color] LIKE ""*" & u & "*"") AND "
End If
If Me.BC = True Then
strwhere = strwhere & "([color] LIKE ""*" & b & "*"") AND "
End If
If Me.GC = True Then
strwhere = strwhere & "([color] LIKE ""*" & g & "*"") AND "
End If
If Me.WC = True Then
strwhere = strwhere & "([color] LIKE ""*" & w & "*"") AND "
End If
lng = Len(strwhere) - 5
If lnglen <= 0 Then
Else
strwhere = Left$(strwhere, lnglen)
Debug.Print strwhere
Me.Filter = strwhere
Me.FilterOn = True
End If
End Sub
Private Sub STBN_CLICK()
requeryform
End Sub
Undefined variable error is due to not enclosing literal text within quote marks and using lng instead of lnglen. Consider revised code:
Dim strGRUBW As String
If Not IsNull([Forms]![sbx]!CTB) Then
strwhere = strwhere & "([CARD] LIKE ""*" & [Forms]![sbx]!CTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!OTB & [Forms]![sbx]!CTTB & [Forms]![sbx]!ARTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!TTB) Then
strwhere = strwhere & "([ctype] LIKE ""*" & [Forms]![sbx]!TTB & "*"") AND "
End If
With Me
strGRUBW = IIf(.RC, "r,", "") & IIf(.UC, "u,", "") & IIf(.BC, "b,", "") & IIf(.GC, "g,", "") & IIf(.WC, "w,", "")
End With
If strGRUBW <> "" Then strwhere = strwhere & "[color] LIKE '*[" & strGRUBW & "*]' AND "
lnglen = Len(strwhere) - 5

Use one filter value to search and output several fields

Using Visual Basic.
Having searched and searched for an answer, my filter only selects from the 'Recipe' field. I'd like to input 'egg' into my txtSearch textbox and have my button give all recipes that have 'egg' in the recipe or as text in an ingredient. Currently only outputs 2 results: egg nog and eggs benedict. There are 15 recipes with egg in that I'd like to display, too.
Private Sub Search_Button_Click()
On Error GoTo Search_Button_Click_Err
Dim strSQL As String
strSQL = "[Cocktail] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing1] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing2] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing3] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing4] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing5] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing6] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing7] like '*" & [Forms]![Find]![txtSearch] & "*'"
If Len(strSQL) > 255 Then
MsgBox "ApplyFilter string length exceeds 255 characters"
Else
DoCmd.ApplyFilter "", strSQL
End If
Search_Button_Click_Exit:
Exit Sub
Search_Button_Click_Err:
MsgBox Error$
Resume Search_Button_Click_Exit
End Sub
an option is to concatenate all of the SQL fields that may contain what you're looking for then have that result in the where xxx like ....
Also some SQL environments use a % as a wildcard.

How to sort filtered ListBox

I have created search box for a ListBox in form. Search function works as intended, but I would like results to show in ascending order based on first column. I am having trouble understanding how to do that. My code:
Private Sub searchTB_Change()
Dim strSource As String, strSearch As String
'code for searching records
strSearch = Replace(searchTB.Text, "'", "''")
strSource = "SELECT [ID], [VP_veiklioji], [VP_invented_name], [Pareisk_pav], [Par_gavimo_data], [Finished] " _
& "FROM TerPar_Oldsys " _
& "WHERE [ID] LIKE '*" & strSearch & "*' " _
& "Or [VP_veiklioji] LIKE '*" & strSearch & "*' " _
& "Or [VP_invented_name] LIKE '*" & strSearch & "*' " _
& "Or [Pareisk_pav] LIKE '*" & strSearch & "*' " _
& "Or [Par_gavimo_data] LIKE '*" & strSearch & "*' " _
& "Or [Finished] LIKE '*" & strSearch & "*' " _
'up to this part everything works
'line below do not work (it gets whole code in red in debugger) and I do not know how to fix it
& "ORDER BY" "[ID]" ASC,"
'bottom two lines work too, I have tryed DoCmd.SetOrderBy but do not understand how to make it work either
Me.oldListBox.ColumnWidths = "1.5 cm; 3 cm; 4 cm; 4 cm; 2 cm; 0.6 cm"
Me.oldListBox.RowSource = strSource
End Sub
EDIT: In my opinion it is not duplicate, since I am aiming at totally different architecture which turns out needed only quotes removal as Gustav suggested.
Remove the quotes and the comma:
& "ORDER BY [ID] ASC"

Order By DESC in SQL Query

I have an embedded SQL query in a command button, I now need to add an ORDER BY [Pay_Date] DESC so it orders the most recent dates on top, however I cant seem to get it to work? Here is what I have tried
Private Sub Command1_Click()
Dim strsearch As String
Dim strText As String
strText = Me.txtSearch.Value
strsearch = "Select * from tblInvoiceLog where ((Vendor_Number like ""*" & strText & "*"") or (Vendor_Name like ""*" & strText & "*"") or (Invoice_1 like ""*" & strText & "*"") or (Invoice_2 like ""*" & strText & "*"") or (Invoice_3 like ""*" & strText & "*"") or (Invoice_4 like ""*" & strText & "*"") or (Invoice_5 like ""*" & strText & "*"") or (Check_Request_Total like ""*" & strText & "*"") or (Voucher_Number like ""*" & strText & "*"") or (Notes like ""*" & strText & "*"") ORDER BY ([Pay_Date] DESC))"
Me.RecordSource = strsearch
End Sub
I am getting the Runtime 3075 error

Problems in converting an SQL query into vba rowsource

So I'm having some problems trying to populate a Listbox in Access with an SQL Query. I'm still learning VBA but this is one problem that has stumped me. I've tried fixing up my code but it the AND-date-BETWEEN part doesn't seem to work with my other AND-columnname-LIKE parts.
I was able to get the BETWEEN part to work by itself and the LIKE parts to work by themselves but not together. Here is the primary vb code that I've been trying to modify. Also DQ is another function that helps with the double quotes for the query.
Me.lstGngSheets.RowSource = _
"SELECT gangID, fileName, date, crossStreets FROM tblgangSheets " & _
"WHERE borough LIKE ""*" & DQ(Me.cboBoro.Value) & _
"*"" AND date BETWEEN #" & txtDtBeg.Value & "# and #" & txtDtEnd.Value & "#" & _
"*"" AND onStreet LIKE ""*" & DQ(Me.cboOnStr.Value) & _
"*"" AND yard LIKE ""*" & DQ(Me.cboYrd.Value) & _
"*"" AND safeStreets LIKE ""*" & DQ(Me.chkSenior.Value) & "*"""
Here is an SQL output that I'm trying to aim for to populate my listbox.
SELECT gangID, fileName, date, crossStreets
FROM tblgangSheets
WHERE borough LIKE "Queens"
AND date BETWEEN #01/1/2013# and #1/1/2014#
AND onStreet LIKE "**"
AND yard LIKE "**"
AND safeStreets LIKE "**";
I've been wracking my brain over this for a while, if any of you can give me advice or a solution, I would definitely appreciate it!
YOu have some weird spacing in your SQL between the date and the like's. Try this.
Me.lstGngSheets.RowSource = "SELECT gangID, fileName, date, crossStreets FROM tblgangSheets " & _
"WHERE borough LIKE ""*" & DQ(Me.cboBoro.Value) & _
"*"" AND date BETWEEN #" & txtDtBeg.Value & "# and #" & txtDtEnd.Value & "#" & _
" AND onStreet LIKE ""*" & DQ(Me.cboOnStr.Value) & _
"*"" AND yard LIKE ""*" & DQ(Me.cboYrd.Value) & _
"*"" AND safeStreets LIKE ""*" & DQ(Me.chkSenior.Value) & "*"""
Note that I removed the "*"" immediately after the # closing your end date.