View Single Post
Old 04-11-2007, 12:55 AM   #6 (permalink)
wolf99
Code Monkey
 
wolf99's Avatar
 
Join Date: Jan 2006
Location: in college (again) in rural Eire
Posts: 45
wolf99 is on a distinguished road
me dense.

OK well I dont see the lookup fields you mention, so guess this has to be coded.
problem is I really know pretty much nothing about VB (or access), having come from C and assembly and am trying to cobble my learning together from old VB programs as I go along.

I got some code from a site:
Code:
Public Sub PopulateLBWithData(DBPath As String, _
TableName As String, FieldName As String, _
oListControl As Object,Optional Distinct As Boolean = False, _
Optional OrderBy As String)

'PURPOSE: Populate a list box, combo box
'or control with similar interface with data
'from one field in a Access Database table

'Parameters: DBPath: FullPath to Database
'TableName: The Name of the Table
'FieldName: Name of the Field
'Distinct: Optional -- True if you want distinct value
'Order By:  Optional -- Field to Order Results by

'Must have reference to DAO in your project

Dim sSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim td As DAO.TableDef
Dim f As DAO.Field

Dim sTest As String
Dim bIsNumeric As Boolean
Dim i As Integer

On Error Resume Next

'validate all parameters
    
oListControl.AddItem "a"
oListControl.Clear
If Err.Number > 0 Then Exit Sub

sTest = Dir(DBPath)
If sTest = "" Then Exit Sub

Set db = Workspaces(0).OpenDatabase(DBPath)
If Err.Number > 0 Then Exit Sub

Set td = db.TableDefs(TableName)
If Err.Number > 0 Then
    db.Close
    Exit Sub
End If

Set f = td.Fields(FieldName)
    If Err.Number > 0 Then
        db.Close
        Exit Sub
    End If

If Len(OrderBy) Then
    Set f = td.Fields(OrderBy)
    If Err.Number > 0 Then
        db.Close
        Exit Sub
    End If
End If
    
sSQL = "SELECT "
If Distinct Then sSQL = sSQL & "DISTINCT "
sSQL = sSQL & "[" & FieldName & "] FROM [" & TableName & "]"

If OrderBy <> "" Then sSQL = sSQL & " ORDER BY " & OrderBy

Set rs = db.OpenRecordSet(sSQL, dbOpenForwardOnly)

With rs
    Do While Not .EOF
        oListControl.AddItem rs(FieldName)
        .MoveNext
    Loop
    .Close
End With

db.Close
End Sub
but I really have no clue what half of it means. Do I need to use SQL? is there a shorter way to do this (it seems to me there should be)? etc, etc.
(Thanks for your hints btw)
If you could even point me in the right direction, maybe a tutroial site that would include this? I searched MSDn, google, etc for info on lookup box, but became more confused

thank you for taking the time to help me, it is very much appreciated.
wolf99 is offline   Reply With Quote