So I press the button, annnnndddd..... NUTHIN'!

G

Guest

This is a continuation of the question I had answered here
http://www.microsoft.com/office/com...af5f&mid=9c9f7f78-c3e8-4b5c-9746-c806c0234a77

I incorporated the changes suggested, compiled everything and made sure all
my form properties were set to YES for the form. According to my code, I
thought that I would get a message box with the resulting SQL code appearing,
instead, I get this brief "blink" for the form and then nothing happens.
Nada. Zippo. Anyone have any ideas as to what might be wrong?
 
G

Guest

Hi J,


-- PROBLEMS I see--

There is a typo in the function "Function BuildSQLString(strSQL As String)
As Boolean", in this line:

strSELECT = "tblMaim.* "


It should be tblmaiN.* with an "N" not "M".


And in the Sub "", you should have already changed

Dim stDocName As String

to

Dim strSQL As String


In the function "BuildSQLString", it looks like there might be some values
that are text. If so, they need to be delimited with (single) quotes.....

If chkToolName Then
strWHERE = strWHERE & " AND tblMain.tool_nomen = " & txtToolName
End If

if "txtToolName" is text then is should be

strWHERE = strWHERE & " AND tblMain.tool_nomen = '" & txtToolName & "'"


(after the equal sign it is single quote/ double quote
and at the end of the line it is double quote/single quote/ double quote)

----------------------------------------------------
Have you tried to step thru the code to see what it does?? In the IDE, click
on the line

Private Sub cmdSearch_Click()


then press the F9 key. The line should now have a brownish background and a
brown dot to the left. This is a breakpoint; the code will stop executing at
this point to allow you to step thru the code one line at a time (using the
F8 key) and see what values are in the variables.

If you click on the dot, the breakpoint will be removed. Click in the gray
area where the dot was to set the breakpoint again.

BTW, you could do all of this a lot easier in one subroutine....but for
learning sake, I'll use your code...

-----------------------------------------------
So, looking at the subroutine "Sub cmdSearch_Click()", what is
"EntriesValid"???? I'm guessing this is where your code takes the off ramp...

Then we come to this line:

If Not BuildSQLString(strSQL) Then

This says you are passing a variable to a function which returns a boolean.
But right now the function
"Function BuildSQLString(strSQL As String) As Boolean" ALWAYS returns
TRUE because of this line:

BuildSQLString = True




I modified your code a little...... see if it makes sense to you

Private Sub cmdSearch_Click()

' Dim stDocName As String
Dim strSQL As String

' commented out
' If Not EntriesValid Then Exit Sub

' If Not BuildSQLString(strSQL) Then
' MsgBox "Something is all screwed up"
' Exit Sub
' End If

strSQL = BuildSQLString

MsgBox strSQL

CurrentDb.QueryDefs("qryModDef").SQL = strSQL

End Sub
'-------

' Function BuildSQLString(strSQL As String) As Boolean
Function BuildSQLString() As string


' Dim strSELECT As String
' Dim strFROM As String
Dim strSQL As String
Dim strWHERE As String

' next line has a typo
' strSELECT = "tblMaim.* "
' strFROM = "tblMain "

strSQL = "SELECT tblMain.* FROM tblMain"

If chkToolNum Then
strWHERE = strWHERE & " AND tblMain.tool_number = " & txtToolNum
End If

If chkToolName Then
' needs to be delimited if txtToolName is text
strWHERE = strWHERE & " AND tblMain.tool_nomen = " & txtToolName
End If

If chkCategory Then
strWHERE = strWHERE & " AND tblMain.tool_category = " & ddmCategory
End If

If chkControlCode Then
strWHERE = strWHERE & " AND tblMain.tool_control_code = " & ddmCategory
End If

If chkPriority Then
strWHERE = strWHERE & " AND tblMain.tool_release_priority = " &
ddmPriority
End If

If chkStatus Then
strWHERE = strWHERE & " AND tblMain.tool_status = " & ddmStatus
End If

If chkToolEng Then
strWHERE = strWHERE & " AND tblMain.tool_eng = " & ddmToolEng
End If

If chkMfgEng Then
strWHERE = strWHERE & " AND tblMain.manuf_eng = " & ddmMfgEng
End If

If chkTDRNum Then
strWHERE = strWHERE & " AND tblMain.tdr_number = " & txtTDRNum
End If

If chkTDRType Then
strWHERE = strWHERE & " AND tblMain.tdr_type = " & ddmTDRType
End If

If chkSupplier Then
strWHERE = strWHERE & " AND tblMain.tool_supplier = " & ddmSupplier
End If

If chkDueDate Then
If Not IsNull(txtDateFrom) Then
strWHERE = strWHERE & " AND tblMain.tool_reqd_date >= #" &
Format$(txtDateFrom, "mm/dd/yyyy") & "#"
End If
If Not IsNull(txtDateTo) Then
strWHERE = strWHERE & " AND tblMain.tool_reqd_date <= #" &
Format$(txtDateTo, "mm/dd/yyyy") & "#"
End If
End If

If chkOldTool Then
strWHERE = strWHERE & " AND tblMain.old_tool_number = " & txtOldTool
End If

' strSQL = "SELECT " & strSELECT
' strSQL = strSQL & "FROM " & strFROM
If strWHERE <> "" Then strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)

'return the SQL string
BuildSQLString = strSQL

End Function


HTH
 
G

Guest

Hi Steve,

Thank you for responding to this one. I tried making the changes to all of
my text box choices, but now when I run it, I get a run time error that says
"Invalid use of null" in my BuildSQLString function at this line:


If chkToolNum Then
strWHERE = strWHERE & " AND tblMain.tool_number = '" & txtToolNum & "'"
End If

I went back and made sure that I didn't have any other typos for my variable
names or objects. Do you know what might be causing that to happen?
 
G

Guest

Oh, just so you have the latest version of what the code has morphed into:
=====================================

Option Explicit
'-----------------------------------
Function BuildSQLString() As String

Dim strSQL As String

strSQL = "SELECT tblMain.* FROM tblMain "

Dim strWHERE As String

If chkToolNum Then
strWHERE = strWHERE & " AND tblMain.tool_number = '" & txtToolNum & "'"
End If

If chkToolName Then
strWHERE = strWHERE & " AND tblMain.tool_nomen = '" & txtToolName & "'"
End If

If chkCategory Then
strWHERE = strWHERE & " AND tblMain.tool_category = " & ddmCategory
End If

If chkControlCode Then
strWHERE = strWHERE & " AND tblMain.tool_control_code = " & ddmCategory
End If

If chkPriority Then
strWHERE = strWHERE & " AND tblMain.tool_release_priority = " & ddmPriority
End If

If chkStatus Then
strWHERE = strWHERE & " AND tblMain.tool_status = " & ddmStatus
End If

If chkToolEng Then
strWHERE = strWHERE & " AND tblMain.tool_eng = " & ddmToolEng
End If

If chkMfgEng Then
strWHERE = strWHERE & " AND tblMain.manuf_eng = " & ddmMfgEng
End If

If chkTDRNum Then
strWHERE = strWHERE & " AND tblMain.tdr_number = '" & txtTDRNum & "'"
End If

If chkTDRType Then
strWHERE = strWHERE & " AND tblMain.tdr_type = " & ddmTDRType
End If

If chkSupplier Then
strWHERE = strWHERE & " AND tblMain.tool_supplier = " & ddmSupplier
End If

If chkDueDate Then
If Not IsNull(txtDateFrom) Then
strWHERE = strWHERE & " AND tblMain.tool_reqd_date >= " & "#" &
Format$(txtDateFrom, "mm/dd/yyyy") & "#"
End If
If Not IsNull(txtDateTo) Then
strWHERE = strWHERE & " AND tblMain.tool_reqd_date <= " & "#" &
Format$(txtDateTo, "mm/dd/yyyy") & "#"
End If
End If

If chkOldTool Then
strWHERE = strWHERE & " AND tblMain.old_tool_number = '" & txtOldTool & "'"
End If

If strWHERE <> "" Then strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)

BuildSQLString = strSQL

End Function

'--------------------------------------------------------------------
Private Sub cmdSearch_Click()
On Error GoTo Err_cmdSearch_Click

Dim strSQL As String

strSQL = BuildSQLString

MsgBox strSQL

CurrentDb.QueryDefs("qryModDef").SQL = strSQL


Err_cmdSearch_Click:
MsgBox Err.Description

End Sub

'-------------------------------------------------
Private Sub butCancel_Click()
On Error GoTo Err_butCancel_Click

Dim stDocName As String

stDocName = "macCancelSearch"
DoCmd.RunMacro stDocName

Exit_butCancel_Click:
Exit Sub

Err_butCancel_Click:
MsgBox Err.Description
Resume Exit_butCancel_Click

End Sub
 
G

Guest

Since I can't see your mdb, I have to ask more questions....

In the function "BuildSQLString()", in the IF() functions, is anything that
starts with "chk" a check box control (like chkToolNum) ???

What data types are the fields in tblMain that are listed in the code??
ie, is "tblMain.tool_number" a number - Long or is it Text?
Same for "tblMain.tool_nomen"....
And tblMain.tool_category"
and the rest of the fields in the function.
(I can't tell from your naming convention what the datatypes of the fields
are)


Numbers do not need delimited
Strings (text) need to be delimited with quotes
Dates are delimited with #


I'm guessing since this code involves searching, the controls like
"txtToolNum" and "txtToolName" are unbound and are used to enter values you
want to search for.

What is the purpose of controls like "chkToolNum" and "chkToolName"?
 
G

GH

There are still some problems with this code. Just off the top of my
head, your checkbox validations are not correct. You are treating
each checkbox as a boolean value, but that is not how it is recognized
in VBA. Example syntax should be:

If chkToolNum.Value <> ""
strWHERE = strWHERE & " AND tblMain.tool_number = '" &
txtToolNum.Value & "'"
End If

Also, since each condition adds an " AND ..." to your WHERE clause,
you need to initialize the variable with an actual valid WHERE clause
that will not filter any records, such as strWHERE = " WHERE 1 = 1".

Hope this gets you moving forward again.
- GH
 
G

Guest

Hi Steve,

Again, I really appreciate you helping out a non-programmer type with this
(especially since I'm about as good with coding as the Marquis de Sade is at
running a daycare). I could email my database to you for you to look at
(it's currently "empty", and has no proprietary information) if that would be
faster. If not, I can let you know what my naming convention is. My
prefixes are:
ddm for Combo Box (I always look at cbo and think checkbox, so I think of
them instead as Drop Down Menus)
chk for Check Box
txt for Text Fields
str for String variables

All of the fields (except for the txtDateFrom and txtDateTo variables) are
text values. Most are populated on the form from combo boxes linked to other
tables (such as tblControlCode for the Control Code value, etc).
 
G

Guest

If you email me the mdb, it would help to have a couple of records with data.

Do a "Compact and Repair" then WinZip it before you send it.

I'll take a look at it as soon asI can...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top