Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class frmMain
  Inherits System.Windows.Forms.Form

  Private Const MAX_RECORDS As Integer = 10000

  Private RecordsRead As Long

  Private strConnect As String
  Private MyTable As String
  Private MyDataSet As New DataSet()
  Private Function GetFileName() As String
    ' Przeznaczenie: Ta funkcja próbuje otworzyć bazę danych Access 
    '
    ' Lista argumentów:
    '  brak
    '
    ' Zwracana wartość
    '  string   nazwa wskazanego pliku albo pusty łańcuch znaków 
    '        w przypadku anulowania.
    '
    Dim NewFileName As String
    Dim NewFile As OpenFileDialog = New OpenFileDialog()
    Dim MyChoice As DialogResult

    With NewFile
      .Filter = "Access Files(*.mdb)|*.mdb" & _
                "|All Files (*.*)|*.*"
      .FilterIndex = 1        ' Przyjmujemy domyślnie pliki bazy Access
      .DefaultExt = "mdb"     ' Jak wyżej
      .InitialDirectory = "D:\VSNET\Crystal Reports\Samples\Database\"
      .CheckFileExists = False
      .ReadOnlyChecked = True
      .Title = "Open Access Data File"
    End With

    MyChoice = NewFile.ShowDialog      ' Co wybrał użytkownik?

    If MyChoice = DialogResult.Cancel Then ' Zrezygnował?
      NewFileName = ""
    Else
      NewFileName = NewFile.FileName
    End If

    If NewFileName.Length > 0 Then
      Return NewFileName
    End If
  End Function

  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles MyBase.Load
    ' Przeznaczenie: Otwarcie bazy danych i przeczytanie z niej nazw 
    ' tabel.
    Dim FileName, SQL As String

    FileName = GetFileName()      ' Pobierz nazwę pliku bazy danych
    If FileName.Length = 0 Then
      Exit Sub
    End If
    Me.Text = "SQL Tester: " & FileName ' Pokaż nazwę pliku

    ' Tu budujemy łańcuch połączenia. Będzie on używany również później.
    strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User" & _
             "ID=Admin;Data Source="
    strConnect &= FileName
    MyTable = "Customer"
    Dim Connect As New OleDbConnection(strConnect)' Używamy ponownie   
                                                    ' łańcucha połączenia...
    Connect.Open()              ' ...i otwieramy je jeszcze raz
    SQL = "SELECT * FROM Customer"
    Dim MyAdapter As New OleDbDataAdapter(SQL, Connect)

    Try
      ' Ile rekordów zostało zwróconych?
      RecordsRead = MyAdapter.Fill(MyDataSet, 0, MAX_RECORDS, MyTable)
      Connect.Close()
      SetControlBindings()
      MyAdapter = Nothing      ' Zwalniamy zasoby
      Connect = Nothing
    Catch
      MessageBox.Show("Something wrong during connecting.")
                 ' Połączenie nie powiodło się.
    End Try
    WhichRecord()

  End Sub

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnExit.Click
    Me.Dispose()
  End Sub

  Private Sub SetControlBindings()
    txtCompany.DataBindings.Add("Text", MyDataSet, "Customer.Customer _
            Name")
    txtFirstName.DataBindings.Add("Text", MyDataSet, _
            "Customer.Contact First Name")
    txtLastName.DataBindings.Add("Text", MyDataSet, _
            "Customer.Contact Last Name")
    txtAddress.DataBindings.Add("Text", MyDataSet, "Customer.Address1")
    txtCity.DataBindings.Add("Text", MyDataSet, "Customer.City")
    txtState.DataBindings.Add("Text", MyDataSet, "Customer.Region")
    txtZip.DataBindings.Add("Text", MyDataSet, "Customer.Postal Code")
  End Sub

  Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As _
                  System.EventArgs) Handles btnFirst.Click
    Me.BindingContext(MyDataSet, MyTable).Position = 0
    WhichRecord()
  End Sub

  Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnNext.Click
    Me.BindingContext(MyDataSet, MyTable).Position += 1
    WhichRecord()
  End Sub

  Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As _
                   System.EventArgs) Handles btnPrevious.Click
    Me.BindingContext(MyDataSet, MyTable).Position -= 1
    WhichRecord()
  End Sub

  Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnLast.Click
    Me.BindingContext(MyDataSet, MyTable).Position = RecordsRead - 1
    WhichRecord()
  End Sub

  Private Sub WhichRecord()
    ' Przeznaczenie: Podprogram ten ustala, którego rekordu szukamy 
    ' i wpisuje numer rekordu do właściwości text pola grupy.
    Dim where As Long

    where = Me.BindingContext(MyDataSet, MyTable).Position + 1
    gbxDBNavigation.Text = "Navigate database: Record #" & CStr(where)

  End Sub

End Class
