Avatar billede helpplease Nybegynder
01. maj 2001 - 11:23 Der er 5 kommentarer og
1 løsning

index

Jeg har en listbox fra visual basic. Her kan jeg få et indeksnummer ud.

Kan jeg anvende dette nummer til at slette den modsvarende række i min access tabel? hvordan gøres det?
Avatar billede terry Ekspert
01. maj 2001 - 13:11 #1
    Dim dbs As Database
    Dim qdf As QueryDef
    Dim mySQL As String
   
    Set dbs = OpenDatabase(\"C:\\data.mdb\")
   
    mySQL = \"Delete * from yourtable WHERE field = \" & indeksnummer
   
    Set qdf = dbs.CreateQueryDef(\"\", mySQL)
   
    qdf.Execute
   
    qdf.Close
    Set qdf = Nothing
    dbs.Close
    Set dbs = Nothing
Avatar billede terry Ekspert
01. maj 2001 - 13:13 #2
I am assuming that indeksnummer is a field in your table.
Avatar billede helpplease Nybegynder
01. maj 2001 - 16:07 #3
No I am afraid that index is not a field, if so it would be easy to solve. The problem is that I might delete e.g row 4, and then I have no runningnumber, so therefore I need a way to count the records and delete the right one which corrosponse to the on in the listbox
Avatar billede terry Ekspert
01. maj 2001 - 17:13 #4
Dim dbsNorthwind As Database
Dim rstSuppliers As Recordset
Dim varBookmark As Variant
Dim strCommand As String
Dim lngMove As Long

Set dbsNorthwind = OpenDatabase(\"Northwind.mdb\")
Set rstSuppliers = dbsNorthwind.OpenRecordset(\"SELECT CompanyName, \" & _
        \"City, Country FROM Suppliers ORDER BY CompanyName\", _
        dbOpenDynaset)

With rstSuppliers
\' Populate recordset.
    .MoveLast
    .MoveFirst

Do While True

\' Display information about current record and ask
            \' how many records to move.
            strCommand = InputBox( _
                \"Record \" & (.AbsolutePosition + 1) & \" of \" & _
                .RecordCount & vbCr & \"Company: \" & _
                !CompanyName & vbCr & \"Location: \" & !City & _
                \", \" & !Country & vbCr & vbCr & _
                \"Enter number of records to Move \" & _
                \"(positive or negative).\")

            If strCommand = \"\" Then Exit Do

            \' Store bookmark in case the Move doesn\'t work.

varBookmark = .Bookmark

            \' Move method requires parameter of data type Long.
            lngMove = CLng(strCommand)
            .Move lngMove

            \' Trap for BOF or EOF.
            If .BOF Then
                MsgBox \"Too far backward! \" & _
                    \"Returning to current record.\"
                .Bookmark = varBookmark
            End If
            If .EOF Then
                MsgBox \"Too far forward! \" & _
                    \"Returning to current record.\"
                .Bookmark = varBookmark
            End If

Loop
        .Close
    End With

    dbsNorthwind.Close


   
    qdf.Close
    Set qdf = Nothing
    dbs.Close
    Set dbs = Nothing
Avatar billede terry Ekspert
01. maj 2001 - 17:17 #5
The example is taken from Access help.
From it you can see how you move around a recordset. Once you get to the correct record you can use the delete method:

rstSuppliers.delete

Oh! and the last four  lines of the previous example can be removed.


Avatar billede helpplease Nybegynder
02. maj 2001 - 08:22 #6
Thank you for the help, just one more little think, I have perviously encountert the Bookmark, what is the concept of this, do you know? I can\'t quite grasp it.... ;)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester