Thanks for your help...
There is a list box with the following code.
SELECT MsysObjects.Name AS ObjectName, IIf([type]=1 Or
[type]=6,"Table","Query") AS ObjectType
FROM MsysObjects
WHERE (((Left$([Name],1))<>"~") AND ((Left$([Name],4))<>"Msys") AND
((MsysObjects.Type)=1 Or (MsysObjects.Type)=5 Or (MsysObjects.Type)=6) AND
((MsysObjects.Flags)=2097152 Or (MsysObjects.Flags)=128 Or
(MsysObjects.Flags)=0 Or (MsysObjects.Flags)=16))
ORDER BY MsysObjects.Name;
after you choose from the list (it is alphabetical)
Private Sub lstTables_AfterUpdate()
'Try and enable the next control only if the the Clear
'button has been clicked (ctl.Tag = vbNullString)
'Otherwise just requery the field's info
'
Call cmdClear_Click
If Me.lstTables.Tag = vbNullString Then Call fEnableNextInTab
Me.cbxFld0.Requery
End Sub
The "where" cbo box fills...
Private Sub sFillCombo(intTargetIndex As Integer)
'Fills the Rowsource for a combo
'
On Error GoTo ErrHandler
Dim I As Long
Dim J As Integer
Dim strOut As String
Dim ctlTarget As Control
'Which one to fill?
Set ctlTarget = Me("cbxFld" & intTargetIndex)
For I = LBound(mvarOriginalFields) To UBound(mvarOriginalFields)
strOut = strOut & mvarOriginalFields(I) & ";"
Next
With ctlTarget
.RowSourceType = "Value List"
.RowSource = strOut
End With
ExitHere:
Set ctlTarget = Nothing
Exit Sub
ErrHandler:
Resume ExitHere
End Sub
then when you choose from where cbo(above) the "criteria" txtbox you can
type in .. then there is a result listbox.
Sub sBuildSQL()
' Take what's currently selected on the form
' and create a dynamic SQL statement for the
' lstResults listbox.
'
On Error GoTo ErrHandler
Dim strSql As String
Dim strWhere As String
Dim strJoinType As String
Dim I As Integer
Dim J As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim tdf As TableDef
Dim qdf As DAO.QueryDef
Dim rsQdf As DAO.Recordset
Dim fld As DAO.Field
Const conMAXCONTROLS = 5
Set db = DBEngine(0)(0)
strSql = "Select * "
'Right now we have five combo/textbox sets
'so set up the master loop to go through these controls
For I = 0 To conMAXCONTROLS - 1
strJoinType = vbNullString
'there might be some unused sets, so don't bother
'going through the disabled controls
If Me("cbxFld" & I).Enabled Then
'The Or/And set starts with the second combo/textbox set
'so if there's only one criteria specified, don't need to
'concatenate additional stuff.
If I > 0 Then
If Me("opgClauseType" & I) = 1 Then
strJoinType = " OR "
Else
strJoinType = " AND "
End If
End If
'Get the a reference to the field in the table/Query as
'we'll need it for BuildCriteria later on
If Me.lstTables.Column(1) = "Table" Then
Set tdf = db.TableDefs(Me.lstTables.Column(0))
Set fld = tdf.Fields(Me("cbxFld" & I))
Else
Set rsQdf = db.OpenRecordset( _
"Select * from [" & Me.lstTables.Column(0) & "] Where 1=2",
dbOpenSnapshot)
Set fld = rsQdf.Fields(Me("cbxFld" & I))
End If
'Only build a criteria if something's typed in the textbox
'otherwise assume all records
If Not IsNull(Me("txtVal" & I)) Then
strWhere = strWhere & strJoinType & Application.BuildCriteria( _
"[" & Me("cbxFld" & I) &
"]", _
fld.Type, Me("txtVal" & I) & "")
Else
strWhere = strWhere & strJoinType & "[" & Me("cbxFld" & I) & "]
like '*'"
End If
End If
Next
'The final all important SQL statement
strSql = strSql & " from [" & Me.lstTables & "] Where " & strWhere
'If the user has modified the SQL directly, take what they've typed in
If Nz(Me.chkEditSQL, False) = False Then
'"save" it in the textbox
Me.txtSQL = strSql
End If
With Me.lstResult
Set rs = db.OpenRecordset(Me.txtSQL)
'assign the SQL to the lstResult only if
' (a) it's valid (Set rs will generate an error otherwise)
' (b) if the recordset actually returned any records.
If rs.RecordCount > 0 Then
Me.cmdCopySQL.Enabled = True
Me.cmdCreateQDF.Enabled = True
Me.cmdExport.Enabled = True
.RowSourceType = "Table/Query"
.RowSource = Me.txtSQL
.Enabled = True
'display * fields
.ColumnCount = CInt(Me.lstTables.Tag)
.ColumnHeads = True
Me.chkEditSQL.Enabled = True
Else
'Thanks for trying, better luck next time!!
Me.cmdCopySQL.Enabled = False
Me.cmdCreateQDF.Enabled = False
Me.cmdExport.Enabled = False
.ColumnCount = 1
.RowSourceType = "Value List"
.RowSource = "No records found."
End If
End With
ExitHere:
Set rsQdf = Nothing
Set rs = Nothing
Set tdf = Nothing
Set db = Nothing
Exit Sub
ErrHandler:
Select Case err.Number
'we're trying to open a parameter query
Case 3061:
MsgBox "The " & mconQ & Me.lstTables & mconQ & " query you've
selected " _
& " is a Parameter Query." & vbCrLf & err.Description,
vbExclamation + vbOKOnly, _
"Missing parameters"
Case Else:
'Either invalid SQL or some other error
End Select
Me.cmdCopySQL.Enabled = False
Me.cmdCreateQDF.Enabled = False
With Me.lstResult
.RowSourceType = "Value List"
.RowSource = "Invalid SQL statement."
.ColumnHeads = False
.ColumnCount = 1
.Enabled = False
End With
Resume ExitHere
End Sub
------
then the fListFill code is next..
Dirk Goldgar said:
lmv said:
The source is ALL of the tables and query's in my mdb. You choose a
table and it poplulates the cbo. It is just pulling the info the way
it is listed in the table and there is no way to change the order...
I guess.
Function fListFill(ctl As Control, varID As Variant, lngRow As Long, _
lngCol As Long, intCode As Integer) As Variant
'The callback function for the first combo
' sFillCombo takes care of the rest of 'em.
On Error GoTo ErrHandler
Static sastrObjSource() As String
Static sastrFields() As String
Static slngCount As Long
Static sdb As DAO.Database
Dim I As Long
Dim J As Long
Dim tdf As TableDef
Dim rsQdf As DAO.Recordset
Dim fld As DAO.Field
Dim varRet As Variant
Dim strObjectType As String
Dim varItem As Variant
Select Case intCode
Case acLBInitialize
If sdb Is Nothing Then Set sdb = CurrentDb
With Me
ReDim sastrObjSource(0)
'Are we looking for a table or a query
sastrObjSource(0) = .lstTables.Column(0)
strObjectType = .lstTables.Column(1)
J = -1
If strObjectType = "Table" Then
Set tdf = sdb.TableDefs(sastrObjSource(0))
Me.lstTables.Tag = tdf.Fields.Count
'Get a list of all the fields
For Each fld In tdf.Fields
J = J + 1
ReDim Preserve sastrFields(J)
sastrFields(J) = fld.Name
Next
J = UBound(sastrFields)
Else
'Since the fieldnames can be changed, safest way is to
'open a recordset and go through it's Fields collection
Set rsQdf = sdb.OpenRecordset( _
"Select * from [" & sastrObjSource(0) & "] Where
1=2", _ dbOpenSnapshot)
Me.lstTables.Tag = rsQdf.Fields.Count
For Each fld In rsQdf.Fields
J = J + 1
ReDim Preserve sastrFields(J)
sastrFields(J) = fld.Name
Next
J = UBound(sastrFields)
End If
'sort the string
Call apiSortStringArray(sastrFields)
slngCount = UBound(sastrFields) + 1
'create a module level variant array for other combos
mvarOriginalFields = sastrFields
End With
varRet = True
Case acLBOpen
varRet = Timer
Case acLBGetRowCount
varRet = slngCount
Case acLBGetValue
varRet = sastrFields(lngRow)
Case acLBEnd
Set rsQdf = Nothing
Set tdf = Nothing
Set sdb = Nothing
Erase sastrFields
Erase sastrObjSource
End Select
fListFill = varRet
ExitHere:
Exit Function
ErrHandler:
Resume ExitHere
End Function [...]
Private Sub sFillCombo(intTargetIndex As Integer)
'Fills the Rowsource for a combo
'
On Error GoTo ErrHandler
Dim I As Long
Dim J As Integer
Dim strOut As String
Dim ctlTarget As Control
'Which one to fill?
Set ctlTarget = Me("cbxFld" & intTargetIndex)
For I = LBound(mvarOriginalFields) To UBound(mvarOriginalFields)
strOut = strOut & mvarOriginalFields(I) & ";"
Next
With ctlTarget
.RowSourceType = "Value List"
.RowSource = strOut
End With
ExitHere:
Set ctlTarget = Nothing
Exit Sub
ErrHandler:
Resume ExitHere
End Sub
------------
Private Sub lstTables_AfterUpdate()
'Try and enable the next control only if the the Clear
'button has been clicked (ctl.Tag = vbNullString)
'Otherwise just requery the field's info
'
Call cmdClear_Click
If Me.lstTables.Tag = vbNullString Then Call fEnableNextInTab
Me.cbxFld0.Requery
End Sub
I believe there's still something missing here. This is what I see from
the code you've posted:
+ When you click a row in lstTables, cbxFld0 (I guess) is filled with
the list of fields defined in that table or query. The list is sorted
alphabetically. That list is also preserved in the global array
"mvarOriginalFields".
+ The function sFillCombo would fill one of the numbered "cbxFldn" combo
boxes with the same list of fields, as far as I can tell, by building a
value list string from the array mvarOriginalFields and assigning it to
the combo's rowsource.
+ However, from your description, that doesn't sound like what you had
in mind, and ...
+ You haven't posted any code that actually calls sFillCombo, anyway.
So clearly there's something missing.
Are these remaining combo boxes really supposed to show field lists? Or
are they, as I'd expect from what you've said, supposed to show lists of
values from a particular field in a specified table? If they're
supposed to show values, I don't see how this code could possibly do it.
If the combos *are* showing you field values, but they're just not
sorted the way you want, please post the code that assigns the
rowsources of those combos. Then maybe we can come up with a fix.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)