0

I was just wondering how I would display a random entity in an access column.

Imports System.Data.OleDb
Public Class ReviseFlashcards

Dim connection As New OleDb.OleDbConnection(
  "provider=microsoft.ACE.OLEDB.12.0;Data Source=flashcard login.accdb")
Dim dt As New DataTable
Dim dataadapter As OleDb.OleDbDataAdapter
'contains the current row number
Dim rownumber As Integer = 0
'Data table to contain all the records

Private Sub ReviseFlashcards_Load(sender As Object, e As EventArgs) _
  Handles MyBase.Load
    Dim sqlstring As String = "select * from flashcards"
    connection.Open()
    dataadapter = New OleDb.OleDbDataAdapter(sqlstring, connection)
    dt.Clear()
    dataadapter.Fill(dt)

    txtFront.Text = dt.Rows(0)(2)
    txtBack.Text = dt.Rows(0)(3)
    connection.Close()
End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) _
  Handles btnDisplay.Click

End Sub

End Class

The current code displays the first row in the database which is shown below. I was wondering if there was a way to display a random row by clicking a button with the front and back matching each other. The access database

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Idk
  • 27
  • 5

1 Answers1

0

Add a random generator to the form. Make shure to declare it as Shared, so that this generator will be created only once. This ensures that it generates unique random number sequencess (see: Why isn't this Random number generation code working?).

Private Shared rand As New Random()

Then create a row index by respecting the actual row count:

Dim index = rand.Next(dt.Rows.Count) ' generates index in the range 0 .. Count - 1
txtFront.Text = dt.Rows(index)(2).ToString()
txtBack.Text = dt.Rows(index)(3).ToString()
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188