Toon posts:

[VBA Access97] Koppelen DAO.recordset aan keuzelijst

Pagina: 1
Acties:
  • 255 views sinds 30-01-2008
  • Reageer

Verwijderd

Topicstarter
Weet iemand hoe je aan een keuzelijst (met invoervak) in een form een DAO recordset kan koppelen.
Dit zodat na het uitvoeren van een query, welke het result in de recordset plaats, de keuzelijst wordt "gevuld" met de rows uit de recordset.

Verwijderd

zoek eens naar "RowSourceType" in je help.
Daar staat een mooi voorbeeld in.

Verwijderd

Topicstarter
Verwijderd schreef op 25 oktober 2002 @ 13:52:
zoek eens naar "RowSourceType" in je help.
Daar staat een mooi voorbeeld in.
Hhmmm... Met de instellingen van de keuzelijst had ik al zitten spelen. Helaas niet met het gewenste resultaat.

Bij RowSourceType kan je kiezen uit tabel/query, lijst met waarden, lijst met velden of Visual Basic-functie.

Tabel/qeury is het niet.
Lijst met waarden ook niet, dat moet je namelijk zelf invoeren vervolgens.
Lijst met velden heb ik geprobeerd en dan de query die leidt tot de recordset waar ik het over had als RowSource, maar dat werkt ook niet...
Visual Basic functie is niet van toepassing hier.

Nog een suggestie?

Verwijderd

Als je een recordset hebt dan zal je een VB functie moeten maken en die moeten koppelen aan je keuze lijstje anders zal je eerst je recordset in een tabel moeten zetten en die koppelen.

Verwijderd

Topicstarter
Verwijderd schreef op 25 oktober 2002 @ 14:13:
Als je een recordset hebt dan zal je een VB functie moeten maken en die moeten koppelen aan je keuze lijstje anders zal je eerst je recordset in een tabel moeten zetten en die koppelen.
Ah.... Dus gewoon een functie de recordset laten returnen en dan in de keuzelijst eigenschappen de functie eraan koppelen...

Ff proberen...

Verwijderd

Was het maar zo simpel, als je nou ff "RowSourceType" in je help intyped en dan bij het voorbeeld de code bekijkt. Dan zie je de functie die je aan de keuzelijst kan koppelen :)

Verwijderd

Topicstarter
Ehm... Waar in Access 97 kan je je VB Functie aan je keuzelijst koppelen?

Verwijderd

Dit staat in mijn help:

Create a list box or combo box that gets its rows from a function

You may want to use this procedure when the values in a list are constantly changing, or if the values aren't stored in a database. For example, you could write a function that fills a list box with a list of dates, each one a specific number of days after the current date.

1 Create the function. For information on creating the function, click .
2 Create the list box or combo box and open its property sheet.
3 In the RowSourceType property box, enter the name of the function. Don't put an equal sign before the function name.
4 Leave the RowSource property box blank.

Verwijderd

Topicstarter
Het is me nu gelukt data uit m'n recordset in de keuzelijst te krijgen. Alleen geeft ie 2 keer dezelfde data weer (er bevinden zich wel 2 verschillende records in de record set).
M'n code:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Function ResultaatLijst(vld As Control, id As Variant, rij As Variant, kol As Variant, code As Variant) As Variant
    
    Dim dbLokaal As DAO.Database
    Dim rsResult As DAO.Recordset
    Dim qryQry As String
    
    'Verbinding met de lokale database
    Set dbLokaal = CurrentDb()
    
    'Ophalen leverancier data
    qryQry = " SELECT * " _
           & " FROM tblLeverancier " _
           & " WHERE lev_Bedrijfsnaam LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Contactpersoon LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Soort LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Plaatsnaam LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " ORDER BY lev_Bedrijfsnaam; "
    Set rsResult = dbLokaal.OpenRecordset(qryQry)
    
    Select Case code
        Case acLBInitialize             ' Initialiseren.
            ResultaatLijst = True
        Case acLBOpen                   ' Openen.
            ResultaatLijst = Timer      ' Unieke id.
        Case acLBGetRowCount            ' Aantal rijen ophalen.
            ResultaatLijst = rsResult.RecordCount
        Case acLBGetColumnCount         ' Aantal kolommen ophalen.
            ResultaatLijst = 1
        Case acLBGetColumnWidth         ' Kolombreedte ophalen.
            ResultaatLijst = -1         ' Standaardbreedte gebruiken.
        Case acLBGetValue               ' Gegevens ophalen.
            ResultaatLijst = rsResult("lev_Bedrijfsnaam")
    End Select
End Function


Enig idee hoe ik nou de verschillende recordsets afdruk, in plaats van 2 keer dezelfde? Het zal wel in een array moeten ofzo, maar wat verwacht access als separator?

Verwijderd

Had je dit voorbeeld ook gezien :
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Function ListMDBs(fld As Control, id As Variant, row As Variant, col As Variant, code As Variant) As Variant
    Static dbs(127) As String, Entries As Integer
    Dim ReturnVal As Variant
    ReturnVal = Null
    Select Case code
        Case acLBInitialize             ' Initialize.
            Entries = 0
            dbs(Entries ) = Dir("*.MDB")
            Do Until dbs(Entries) = "" Or Entries >= 127
                Entries = Entries+1
                dbs(Entries) = Dir

Loop
            ReturnVal = Entries
        Case acLBOpen                       ' Open.
            ReturnVal = Timer               ' Generate unique ID for control.
        Case acLBGetRowCount                ' Get number of rows.
            ReturnVal = Entries
        Case acLBGetColumnCount     ' Get number of columns.
            ReturnVal = 1
        Case acLBGetColumnWidth     ' Column width.
            ReturnVal = -1                  ' -1 forces use of default width.
        Case acLBGetValue                   ' Get data.
            ReturnVal = dbs(row)

Case acLBEnd                        ' End.
            Erase dbs
    End Select
    ListMDBs = ReturnVal
End Function

hierin vullen ze een array met waarden, dat wil jij ook

Verwijderd

Topicstarter
Goed... Ik ben er bijna, alleen toont ie nu "veld1" (ID) en "veld2" (bedrijfsnaam) van de recordset als entries in de keuzelijst en dan alleen van het tweede record... :(

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Function ResultaatLijst(vld As Control, id As Variant, varRij As Variant, varKolom As Variant, varCode As Variant) As Variant
    
    Dim dbLokaal As DAO.Database
    Dim rsResult As DAO.Recordset
    Dim qryQry As String
    Dim intArrayEntry As Integer
    Static a_strResultaatArray(127) As String
    Dim varReturnValue As Variant
    
    'Verbinding met de lokale database
    Set dbLokaal = CurrentDb()
    
    'Ophalen leverancier data
    qryQry = " SELECT * " _
           & " FROM tblLeverancier " _
           & " WHERE lev_Bedrijfsnaam LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Contactpersoon LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Soort LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Plaatsnaam LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " ORDER BY lev_Bedrijfsnaam; "
    Set rsResult = dbLokaal.OpenRecordset(qryQry)
    
    Select Case varCode
        Case acLBInitialize             ' Initialiseren.
            intArrayEntry = 0
            a_strResultaatArray(intArrayEntry) = rsResult.RecordCount
            Do Until rsResult.EOF Or intArrayEntry >= 127
                intArrayEntry = intArrayEntry + 1
                a_strResultaatArray(intArrayEntry) = rsResult("lev_Bedrijfsnaam")
                rsResult.MoveNext
            Loop
            varReturnValue = intArrayEntry
        Case acLBOpen                   ' Openen.
            varReturnValue = Timer      ' Unieke id.
            
        Case acLBGetRowCount            ' Aantal rijen ophalen.
            varReturnValue = rsResult.RecordCount
            
        Case acLBGetColumnCount         ' Aantal kolommen ophalen.
            varReturnValue = 1
            
        Case acLBGetColumnWidth         ' Kolombreedte ophalen.
            varReturnValue = -1         ' Standaardbreedte gebruiken.
            
        Case acLBGetValue               ' Gegevens ophalen.
            varReturnValue = rsResult(varRij)
    End Select
    
    ResultaatLijst = varReturnValue
End Function


Ziet iemand het? *zucht*...

Verwijderd

varReturnValue = rsResult(varRij)
moet zijn
varReturnValue = a_strResultaatArray(varRij)

Verwijderd

Topicstarter
Verwijderd schreef op 25 oktober 2002 @ 16:16:
varReturnValue = rsResult(varRij)
moet zijn
varReturnValue = a_strResultaatArray(varRij)
Mmmhh.. In plaats van
2
bedrijfsnaam_2

komt er nu
[leeg]
bedrijfsnaam_2

in de keuzelijst te staan...

Verwijderd

Toch zou ik het volgende even wijzigen:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Case acLBInitialize             ' Initialiseren.
            intArrayEntry = 0
            a_strResultaatArray(intArrayEntry) = rsResult.RecordCount
            Do Until rsResult.EOF Or intArrayEntry >= 127
                intArrayEntry = intArrayEntry + 1
                a_strResultaatArray(intArrayEntry) = rsResult("lev_Bedrijfsnaam")
                rsResult.MoveNext
            Loop
            varReturnValue = intArrayEntry
        Case acLBOpen                   ' Openen.
            varReturnValue = Timer      ' Unieke id.
            
        Case acLBGetRowCount            ' Aantal rijen ophalen.
            varReturnValue = rsResult.RecordCount


in
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Case acLBInitialize             ' Initialiseren.
            intArrayEntry = 0
            a_strResultaatArray(intArrayEntry) = rsResult.RecordCount
            Do Until rsResult.EOF Or intArrayEntry >= 127
                intArrayEntry = intArrayEntry + 1
                a_strResultaatArray(intArrayEntry) = rsResult("lev_Bedrijfsnaam")
                rsResult.MoveNext
            Loop
            rsResult.close
            set rsResult = nothing
            set dbLokaal = nothing
            varReturnValue = intArrayEntry
        Case acLBOpen                   ' Openen.
            varReturnValue = Timer      ' Unieke id.
            
        Case acLBGetRowCount            ' Aantal rijen ophalen.
            varReturnValue = intArrayEntry + 1


Beetje zonde om alles open te laten staan. Je hebt het alleen maar nodig in de initialisatie van je combobox

Verwijderd

Topicstarter
Het is gelukt... Er zaten wat foutjes bij het initialiseren van de keuzelijst. Bij deze de code:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Function ResultaatLijst(vld As Control, id As Variant, varRij As Variant, varKolom As Variant, varCode As Variant) As Variant
    
    Dim dbLokaal As DAO.Database
    Dim rsResult As DAO.Recordset
    Dim qryQry As String
    Dim intArrayEntry As Integer
    Static a_strResultaatArray(127) As String
    Dim varReturnValue As Variant
    
    'Verbinding met de lokale database
    Set dbLokaal = CurrentDb()
    
    'Ophalen leverancier data
    qryQry = " SELECT * " _
           & " FROM tblLeverancier " _
           & " WHERE lev_Bedrijfsnaam LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Contactpersoon LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Soort LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " OR lev_Plaatsnaam LIKE '*" & Forms!frmLeverancier!txtZoeken.Value & "*' " _
           & " ORDER BY lev_Bedrijfsnaam; "
    Set rsResult = dbLokaal.OpenRecordset(qryQry)
    
    Select Case varCode
        Case acLBInitialize             ' Initialiseren.
            'Begin op positie 0 van de array
            intArrayEntry = 0
            'Plaats de items in de array
            Do Until rsResult.EOF Or intArrayEntry >= 127
                a_strResultaatArray(intArrayEntry) = rsResult("lev_Bedrijfsnaam")
                intArrayEntry = intArrayEntry + 1
                rsResult.MoveNext
            Loop
            rsResult.Close
            Set rsResult = Nothing
            Set dbLokaal = Nothing

            'Retourneer het aantal items voor in de keuzelijst
            varReturnValue = intArrayEntry
            
        Case acLBOpen                   ' Openen.
            varReturnValue = Timer      ' Unieke id.
            
        Case acLBGetRowCount            ' Aantal rijen ophalen.
            varReturnValue = rsResult.RecordCount

        Case acLBGetColumnCount         ' Aantal kolommen ophalen.
            varReturnValue = 1
            
        Case acLBGetColumnWidth         ' Kolombreedte ophalen.
            varReturnValue = -1         ' Standaardbreedte gebruiken.
            
        Case acLBGetValue               ' Gegevens ophalen.
            varReturnValue = a_strResultaatArray(varRij)
    End Select
    
    ResultaatLijst = varReturnValue
End Function
Pagina: 1