
Originally Posted by
ExcessionOCP
Thanks both Minty and KD2017. Minty, I have had some success with setting the variables in the general module, but in cases where the SQL query contains a variable that has yet to be set, the queries don't work due to the variable being empty when the string is subsequently used later in my code.
Kd2017, here is a snippet of one of my queries - please excuse the basic and poorly-written nature of these - I know that these queries are subject to possible injection attacks but this isn't a concern at the moment and I plan to move to parametric queries once my understanding improves!
Code:
Private Sub cmbWorkPackageName_AfterUpdate()
ProjectID = Me.cmbProjectName
WPID = Me.cmbWorkPackageName
'Populate the Cost Category combo based on ProjectID and WorkPackage values pulled from the relevant combos
sBudgetlistWP = "SELECT [Projects.ProjectID],[Projects.ProjectName], [CostType.CostTypeID],[CostType.CostTypeDescription], [Budgets.Budget], [Budgets.WorkPackageID] " & _
" FROM CostType " & _
" INNER JOIN (Projects INNER JOIN Budgets ON Projects.ProjectID = Budgets.ProjectID) ON CostType.CostTypeID = Budgets.CostTypeID " & _
" WHERE [Budgets.WorkPackageID] = " & WPID & _
" AND [Projects.ProjectID] = " & ProjectID
Me.lstBudgets.RowSource = sBudgetlistWP
Me.lstBudgets.Requery
End Sub
My ideal solution would be along the lines of writing a function that can have the two variables passed to it, execute the query, then return the data in the query (as a recordset?) which is then used to populate the values of the listbox. I'm not sure if I'm barking up completely the wrong tree here though.
Cheers both.
You could do something like this:
Code:
Public Sub example1(cbo As ComboBox, ProjectID As Long, WPID As Long)
Dim qry As String
qry = "SELECT [Projects.ProjectID],[Projects.ProjectName], [CostType.CostTypeID],[CostType.CostTypeDescription], [Budgets.Budget], [Budgets.WorkPackageID] "
qry = qry & vbCrLf & " FROM CostType "
qry = qry & vbCrLf & " INNER JOIN (Projects INNER JOIN Budgets ON Projects.ProjectID = Budgets.ProjectID) ON CostType.CostTypeID = Budgets.CostTypeID "
qry = qry & vbCrLf & " WHERE [Budgets.WorkPackageID] = " & WPID
qry = qry & vbCrLf & " AND [Projects.ProjectID] = " & ProjectID
'Debug.Print qry 'for testing purposes
cbo.RowSource = qry
cbo.Requery 'I could be wrong but I thought when you change a row source it's automatically requeried??
End Sub
Private Sub cmbWorkPackageName_AfterUpdate()
example1 Me.lstBudgets, Me.cmbProjectName, Me.cmbWorkPackageName
End Sub
Or you could do it this way which would probably be the way I prefer between these two:
Code:
ublic Function example2(ProjectID As Long, WPID As Long) As String
Dim qry As String
qry = "SELECT [Projects.ProjectID],[Projects.ProjectName], [CostType.CostTypeID],[CostType.CostTypeDescription], [Budgets.Budget], [Budgets.WorkPackageID] "
qry = qry & vbCrLf & " FROM CostType "
qry = qry & vbCrLf & " INNER JOIN (Projects INNER JOIN Budgets ON Projects.ProjectID = Budgets.ProjectID) ON CostType.CostTypeID = Budgets.CostTypeID "
qry = qry & vbCrLf & " WHERE [Budgets.WorkPackageID] = " & WPID
qry = qry & vbCrLf & " AND [Projects.ProjectID] = " & ProjectID
example2 = qry
End Function
Private Sub cmbWorkPackageName_AfterUpdate()
Me.lstBudgets.RowSource = example2(Me.cmbprojectname, Me.cmbWorkPackageName)
Me.lstBudgets.Requery
End Sub