shinichi_nguyen
New member
- Local time
- Today, 11:54
- Joined
- Feb 13, 2012
- Messages
- 3
Hi all. I'm creating a simple database for managing parts of machines. I have one form, search form with 4 blank textbox for user to enter. I wrote this code to generate the SQL Select statement. How do I display the result? The Access file only have one table: Parts. Structure as below:
Parts (SerialNum, Description, UseOnMSS, DateOf Failure, DescriptionOfFailure)
key is SerialNum
PS: My programming and VBA skill is about 2/10 so I might wrote this code in silly way, if possible help me improve it. Much appreciate.
Parts (SerialNum, Description, UseOnMSS, DateOf Failure, DescriptionOfFailure)
key is SerialNum
PS: My programming and VBA skill is about 2/10 so I might wrote this code in silly way, if possible help me improve it. Much appreciate.
Code:
Dim SQLstring, myCriteria As String
Dim tSerialNum, tUsedOnMSS, tDateOfFailure, tDescriptionOfFailure As Variant
tSerialNum = txtSerialNum.Value
tUsedOnMSS = txtUsedOnMSS.Value
tDateOfFailure = txtDateOfFailure.Value
tDescriptionOfFailure = txtDescriptionOfFailure.Value
SQLstring = "Select * FROM Parts WHERE "
myCriteria = ""
If tSerialNum <> "" Then
myCriteria = "SerialNum ='" & tSerialNum & "' OR "
Else
myCriteria = myCriteria & ""
End If
If tUsedOnMSS <> "" Then
myCriteria = myCriteria & " UsedOnMSS ='" & tUsedOnMSS & "' OR "
Else
myCriteria = myCriteria & ""
End If
If tDateOfFailure <> "" Then
myCriteria = myCriteria & " DateOfFailure='" & tDateOfFailure & "' OR "
Else
myCriteria = myCriteria & ""
End If
If tDescriptionOfFailure <> "" Then
myCriteria = myCriteria & " DescriptionOfFailure='" & tDescriptionOfFailure & "'"
Else
myCriteria = myCriteria & ""
End If
If myCriteria <> "" Then
If Right(myCriteria, 2) = "OR" Then
myCriteria = Left(myCriteria, Len(myCriteria) - 3)
SQLstring = SQLstring & myCriteria
End If
Else
SQLstring = "Select Parts.SerialNum, Parts.UsedOnMSS Parts.DateOfFailure FROM Parts"
End If
' Now how to put this query string into display???
Last edited: