datacontrol
Registered User.
- Local time
- Today, 23:17
- Joined
- Jul 16, 2003
- Messages
- 142
Hello!
I have a system of forms/subforms and queries set up. I got most of this code from a database already created called "sql example"
I need to revamp my code so that my form works correctly. There are 3 fields (combo boxes) from which the user selects a database item and a subform (appears below the combo boxes) with query results displayed.
So, what I need to do is query upon these fields (the combo boxes). I will need to create some conditional statement that covers all my bases, so that not matter what combination of the three combo boxes are selected, then an appropriate query is generated . For instance, if combo 3 if left null (empty) then the other 2 combos will represent the query and vice cersa, covering all possible combinations.
Please, I know this one is difficult, and I appreciate any help. My db is too large to attach here.
here is my current form (form1) code:
Option Compare Database
Option Explicit
Dim sqlvital, Queryflag, stDocName, SQLbase, SQLtemp, SQLwhole
Dim dbs As Database, qdf As QueryDef, rst As Recordset, Weekdef
Private Sub CmdReset_Click()
Reset
End Sub
Private Sub Combo1_Change()
SQLgen
Refresh
End Sub
Private Sub Combo2_Change()
SQLgen
Refresh
End Sub
Private Sub Combo3_Change()
SQLgen
Refresh
End Sub
Private Sub Combo4_Change()
SQLgen
Refresh
End Sub
Private Sub Form_Load()
DoCmd.Maximize
Reset
End Sub
Sub Reset()
Combo1.Value = ""
Combo2.Value = ""
Combo3.Value = ""
Combo4.Value = ""
SQLgen
Refresh
End Sub
Sub SQLgen()
On Error GoTo Err_SQLgen
' SQL Generation routine
SQLQuerysubform.SourceObject = "Query1subform"
SQLbase = "SELECT tbldst.dst_user, tbldst.dst_task_id, tbldst.dst_date FROM tbldst"
sqlvital = ";"
Queryflag = 0
'Combo1
If Combo1.Value = "" Then
Queryflag = 0
' "Combo1 All"
Else
Queryflag = 1
sqlvital = " WHERE (((tbldst.dst_user)=[Forms]![Form1]![Combo1])"
End If
'Combo2
If Combo2.Value = "" Then
'something
Else
If Queryflag = 1 Then
SQLtemp = " AND ((tbldst.dst_task_id)=[Forms]![Form1]![Combo2])"
LabelSQL.Caption = SQLtemp
sqlvital = sqlvital & SQLtemp
Else
Queryflag = 1
sqlvital = " WHERE (((tbldst.dst_task_id)=[Forms]![Form1]![Combo2])"
End If
End If
'Combo3
If Combo3.Value = "" Then
'something
Else
If Queryflag = 1 Then
SQLtemp = " AND ((tbldst.dst_date)=[Forms]![Form1]![Combo3])"
LabelSQL.Caption = SQLtemp
sqlvital = sqlvital & SQLtemp
Else
Queryflag = 1
sqlvital = " WHERE (((tbldst.dst_date)=[Forms]![Form1]![Combo3])"
End If '
End If
'Combo4
'If Combo4.Value = "" Then
'something
'Else
'If Queryflag = 1 Then
'SQLtemp = " AND ((DATA.HomePhone)=[Forms]![Form1]![Combo4])"
'LabelSQL.Caption = SQLtemp
'sqlvital = sqlvital & SQLtemp
'Else
'Queryflag = 1
'sqlvital = " WHERE (((DATA.HomePhone)=[Forms]![Form1]![Combo4])"
'End If '
'End If
' Delete old Query
stDocName = "SQLQuery"
DoCmd.DeleteObject acQuery, stDocName
' Return reference to current database.
Set dbs = CurrentDb
' Create new query.
Set qdf = dbs.CreateQueryDef("SQLQuery")
' Construct SQL statement including parameters.
' the following is the master SQL, SQLvital contains essential SQL clause
' Depending on selection
If sqlvital <> ";" Then sqlvital = sqlvital & "); "
qdf.SQL = SQLbase & sqlvital
SQLwhole = qdf.SQL
LabelSQL.Caption = SQLwhole
Set dbs = Nothing
SQLQuerysubform.SourceObject = "SQLQuerysubform"
Refresh
Exit_Err_SQLgen:
Exit Sub
Err_SQLgen:
LabelSQL.Caption = SQLbase & sqlvital
MsgBox "Error in SQL formula, try again !", vbCritical, "Error!!!"
End Sub
Private Sub Form_Resize()
SQLQuerysubform.Height = (Me.InsideHeight - SQLQuerysubform.Top) - 1000
SQLQuerysubform.Width = (Me.InsideWidth - 1000)
End Sub
I have a system of forms/subforms and queries set up. I got most of this code from a database already created called "sql example"
I need to revamp my code so that my form works correctly. There are 3 fields (combo boxes) from which the user selects a database item and a subform (appears below the combo boxes) with query results displayed.
So, what I need to do is query upon these fields (the combo boxes). I will need to create some conditional statement that covers all my bases, so that not matter what combination of the three combo boxes are selected, then an appropriate query is generated . For instance, if combo 3 if left null (empty) then the other 2 combos will represent the query and vice cersa, covering all possible combinations.
Please, I know this one is difficult, and I appreciate any help. My db is too large to attach here.
here is my current form (form1) code:
Option Compare Database
Option Explicit
Dim sqlvital, Queryflag, stDocName, SQLbase, SQLtemp, SQLwhole
Dim dbs As Database, qdf As QueryDef, rst As Recordset, Weekdef
Private Sub CmdReset_Click()
Reset
End Sub
Private Sub Combo1_Change()
SQLgen
Refresh
End Sub
Private Sub Combo2_Change()
SQLgen
Refresh
End Sub
Private Sub Combo3_Change()
SQLgen
Refresh
End Sub
Private Sub Combo4_Change()
SQLgen
Refresh
End Sub
Private Sub Form_Load()
DoCmd.Maximize
Reset
End Sub
Sub Reset()
Combo1.Value = ""
Combo2.Value = ""
Combo3.Value = ""
Combo4.Value = ""
SQLgen
Refresh
End Sub
Sub SQLgen()
On Error GoTo Err_SQLgen
' SQL Generation routine
SQLQuerysubform.SourceObject = "Query1subform"
SQLbase = "SELECT tbldst.dst_user, tbldst.dst_task_id, tbldst.dst_date FROM tbldst"
sqlvital = ";"
Queryflag = 0
'Combo1
If Combo1.Value = "" Then
Queryflag = 0
' "Combo1 All"
Else
Queryflag = 1
sqlvital = " WHERE (((tbldst.dst_user)=[Forms]![Form1]![Combo1])"
End If
'Combo2
If Combo2.Value = "" Then
'something
Else
If Queryflag = 1 Then
SQLtemp = " AND ((tbldst.dst_task_id)=[Forms]![Form1]![Combo2])"
LabelSQL.Caption = SQLtemp
sqlvital = sqlvital & SQLtemp
Else
Queryflag = 1
sqlvital = " WHERE (((tbldst.dst_task_id)=[Forms]![Form1]![Combo2])"
End If
End If
'Combo3
If Combo3.Value = "" Then
'something
Else
If Queryflag = 1 Then
SQLtemp = " AND ((tbldst.dst_date)=[Forms]![Form1]![Combo3])"
LabelSQL.Caption = SQLtemp
sqlvital = sqlvital & SQLtemp
Else
Queryflag = 1
sqlvital = " WHERE (((tbldst.dst_date)=[Forms]![Form1]![Combo3])"
End If '
End If
'Combo4
'If Combo4.Value = "" Then
'something
'Else
'If Queryflag = 1 Then
'SQLtemp = " AND ((DATA.HomePhone)=[Forms]![Form1]![Combo4])"
'LabelSQL.Caption = SQLtemp
'sqlvital = sqlvital & SQLtemp
'Else
'Queryflag = 1
'sqlvital = " WHERE (((DATA.HomePhone)=[Forms]![Form1]![Combo4])"
'End If '
'End If
' Delete old Query
stDocName = "SQLQuery"
DoCmd.DeleteObject acQuery, stDocName
' Return reference to current database.
Set dbs = CurrentDb
' Create new query.
Set qdf = dbs.CreateQueryDef("SQLQuery")
' Construct SQL statement including parameters.
' the following is the master SQL, SQLvital contains essential SQL clause
' Depending on selection
If sqlvital <> ";" Then sqlvital = sqlvital & "); "
qdf.SQL = SQLbase & sqlvital
SQLwhole = qdf.SQL
LabelSQL.Caption = SQLwhole
Set dbs = Nothing
SQLQuerysubform.SourceObject = "SQLQuerysubform"
Refresh
Exit_Err_SQLgen:
Exit Sub
Err_SQLgen:
LabelSQL.Caption = SQLbase & sqlvital
MsgBox "Error in SQL formula, try again !", vbCritical, "Error!!!"
End Sub
Private Sub Form_Resize()
SQLQuerysubform.Height = (Me.InsideHeight - SQLQuerysubform.Top) - 1000
SQLQuerysubform.Width = (Me.InsideWidth - 1000)
End Sub