Home > Software design >  How to edit an access database?
How to edit an access database?

Time:11-05

I made a flashcard application that stores the users flashcards into an access database where they will be able to revise from.

Here is part of the code from creating the flashcard

 command = " insert into Flashcards ([Front],[Back],[Difficulty]) values ('" & txtFront.Text & "','" & txtBack.Text & "' ,'" & 3 & "')"

This stores flashcards in the below database Access database

Here is part of the code for revising the flashcards

Private Sub btnEasy_Click(sender As Object, e As EventArgs) Handles btnEasy.Click
        Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
        If txtBack.Visible = True Then 'If the back of the flashcard is shown
            txtFront.Text = dt.Rows(index)(2).ToString() 'Displays a random record in the third column (front of flashcard)
            txtBack.Visible = False 'Does not display the back of the flashcard
            txtBack.Text = dt.Rows(index)(3).ToString() 'Displays a random record in the fourth column ()

        Else 'If the user has not pressed the reveal button before 
            MsgBox("Please first reveal the back of the flashcard")
        End If
    End Sub

    Private Sub btnGood_Click(sender As Object, e As EventArgs) Handles btnGood.Click
        Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
        If txtBack.Visible = True Then
            txtFront.Text = dt.Rows(index)(2).ToString()
            txtBack.Visible = False
            txtBack.Text = dt.Rows(index)(3).ToString()
        Else
            MsgBox("Please first reveal the back of the flashcard")
        End If
    End Sub

    Private Sub btnHard_Click(sender As Object, e As EventArgs) Handles btnHard.Click
        Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
        If txtBack.Visible = True Then
            txtFront.Text = dt.Rows(index)(2).ToString()
            txtBack.Visible = False
            txtBack.Text = dt.Rows(index)(3).ToString()
        Else
            MsgBox("Please first reveal the back of the flashcard")
        End If
    End Sub

Right now btnEasy , btngood and btnhard all do the same thing but i want to know a way that pressing btnEasy for example will change the difficulty from 3 in the database to 1.

Also how would i get it so pressing btneasy will only randomy select flashcards that have a difficulty of 1 and not just randomy select a flashcard from the whole database.

Sorry, if the question is a bit long, ive been stuck on this for quite a while

CodePudding user response:

you will want to filter the Datatable (dt in your code) to select only the records that are relevant to the "Difficulty" for the given button. From your image it seems that Easy = 1, Good = 2, and Hard = 3? If you are unfamiliar with selecting a subset of data from the DataTable, it should look something like this in your code:

    ' Use the Select method to find all rows matching the filter.
    hardRows = dt.Select("Difficulty = 3")

Then you can randomly select from that subset or rows (rather than from dt.Rows.Count).

Also, for more information look this documentation from Microsoft: https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.select

  • Related