Buttons VB don't work and interaction between MS access database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jertos
    New Member
    • Sep 2012
    • 1

    Buttons VB don't work and interaction between MS access database

    Dear all,

    This is my first time on the forum and I'm creating a visual basic GUI in which I can enter certain data and the entered data can and will be saved in the MS access database.

    I created my VB program and linked it with my MS access database, the good thing is, it will load my first row of the database, but unfortunately it doesn't show the following rows. Also my buttons like save, add,... don't Work. Can someone have a quick look and tell me what I'm doing wrong?

    Furthermore is it possible to create a button, which will save a form in my MS access database or is the easiest way to create another new form in VB and print this form?

    Kind regards,

    Jeroen

    Code:
    Class MainWindow
    
            Private testdatabaseData As New testdatabaseDataSet
            Private tatestdatabase As New testdatabaseDataSetTableAdapters.testdatabaseTableAdapter
            Private taManager As New testdatabaseDataSetTableAdapters.TableAdapterManager
            Private View As CollectionView
    
            Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
                    Me.tatestdatabase.Fill(Me.testdatabaseData.testdatabase)
    
                    Me.DataContext = Me.testdatabaseData.testdatabase
    
                    Me.taManager.testdatabaseTableAdapter = tatestdatabase
    
                    Me.DataContext = Me.testdatabaseData.testdatabase
    
                    Me.View = CType(CollectionViewSource.GetDefaultView(Me.testdatabaseData.testdatabase), CollectionView)
    
                    Dim TestdatabaseDataSet As WpfDataBase.testdatabaseDataSet = CType(Me.FindResource("TestdatabaseDataSet"), WpfDataBase.testdatabaseDataSet)
                    'Load data into the table testdatabase. You can modify this code as needed.
                    Dim TestdatabaseDataSettestdatabaseTableAdapter As WpfDataBase.testdatabaseDataSetTableAdapters.testdatabaseTableAdapter = New WpfDataBase.testdatabaseDataSetTableAdapters.testdatabaseTableAdapter()
                    TestdatabaseDataSettestdatabaseTableAdapter.Fill(TestdatabaseDataSet.testdatabase)
                    Dim TestdatabaseViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("TestdatabaseViewSource"), System.Windows.Data.CollectionViewSource)
                    TestdatabaseViewSource.View.MoveCurrentToFirst()
            End Sub
    
            Private Sub cmdfirst_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdfirst.Click
                    Me.View.MoveCurrentToFirst()
    
            End Sub
    
            Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdprevious.Click
                    If Me.View.CurrentPosition > 0 Then
                            Me.View.MoveCurrentToPrevious()
                    End If
    
            End Sub
    
            Private Sub cmdnext_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdnext.Click
                    If Me.View.CurrentPosition < Me.View.Count - 1 Then
                            Me.View.MoveCurrentToNext()
                    End If
    
            End Sub
    
            Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdLast.Click
                    Me.View.MoveCurrentToLast()
            End Sub
    
            Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdDelete.Click
                    If Me.View.CurrentPosition > -1 Then
                            Dim row = CType(Me.View.CurrentItem, System.Data.DataRowView).Row
                            row.Delete()
                    End If
            End Sub
    
            Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdAdd.Click
                    If Not Me.testdatabaseData.HasErrors Then
                            Dim row = Me.testdatabaseData.testdatabase.NewtestdatabaseRow
                            row.Naam = ""
                            Me.testdatabaseData.testdatabase.AddtestdatabaseRow(row)
                            Me.View.MoveCurrentToLast()
                    End If
            End Sub
    
            Private Sub cmdRevert_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdRevert.Click
                    If Me.testdatabaseData.HasChanges Then
                            If MessageBox.Show("Are you sure you want to lose all your changes?", Me.Title, MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
                                    Me.testdatabaseData.RejectChanges()
                            End If
                    End If
    
            End Sub
    
            Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdSave.Click
                    Try
                            If Me.testdatabaseData.HasErrors Then
                                    MsgBox("Please correct the errors first")
                            Else
                                    If Me.testdatabaseData.HasChanges Then
                                            If Me.taManager.UpdateAll(Me.testdatabaseData) > 0 Then
                                                    MsgBox("Saved.")
                                            End If
                                    End If
                            End If
    
                    Catch ex As Exception
                            MsgBox(ex.ToString)
    
                    End Try
    
            End Sub
    
    End Class
    
    Partial Class testdatabaseDataSet
            Partial Class testdatabaseDataTable
    
                    Private Sub CheckNaam(ByVal row As testdatabaseRow)
                            If row.IsNull("Naam") OrElse row.Naam = "" Then
                                    row.SetColumnError(Me.NaamColumn, "Please enter a naam")
    
                            Else
                                    row.SetColumnError(Me.NaamColumn, "")
                            End If
                    End Sub
    
                    Private Sub testdatabaseDataTable_Columnchanged(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanged
                            If e.Column Is Me.NaamColumn Then
                                    Me.CheckNaam(CType(e.Row, testdatabaseRow))
                            End If
    
                    End Sub
    
                    Private Sub testdatabaseDataTable_TableNewRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) Handles Me.TableNewRow
                            Me.CheckNaam(CType(e.Row, testdatabaseRow))
                    End Sub
    
    
    
            End Class
    
    End Class
    Code:
    Class="MainWindow"
            xmlns="[url="http://schemas.microsoft.com/winfx/2006/xaml/presentation"]http://schemas.micro...l/presentation"[/url]
            xmlns:x="[url="http://schemas.microsoft.com/winfx/2006/xaml"]http://schemas.micro...infx/2006/xaml"[/url]
            Title="MainWindow" Height="406" Width="814" xmlns:my="clr-namespace:WpfDataBase">
            <window.Resources>
                    <my:testdatabaseDataSet x:Key="TestdatabaseDataSet" />
                    <CollectionViewSource x:Key="TestdatabaseViewSource" Source="{Binding Path=testdatabase, Source={StaticResource TestdatabaseDataSet}}" />
            </window.Resources>
            <Grid Height="369" Width="770">
                    <Grid.RowDefinitions>
                            <RowDefinition Height="66*" />
                            <RowDefinition Height="303*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200*" />
                            <ColumnDefinition Width="200*" />
                    </Grid.ColumnDefinitions>
                    <Button Content="Save" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="237,0,0,0" Name="cmdSave" VerticalAlignment="Top" Width="75" />
                    <Button Content="Revert" HorizontalAlignment="Right" Margin="0,0,0,43" Name="cmdRevert" Width="75" Grid.Column="1" />
                    <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="164,0,0,0" Name="cmdAdd" VerticalAlignment="Top" Width="75" Grid.Column="1" />
                    <Button Content="Delete" HorizontalAlignment="Left" Margin="112,0,0,43" Name="cmdDelete" Width="55" Grid.Column="1" BorderBrush="#FFFA0000" BorderThickness="3" />
                    <Button Content="|&lt;" Height="23" HorizontalAlignment="Left" Name="cmdfirst" VerticalAlignment="Top" Width="75" />
                    <Button Content="&lt;" Height="23" HorizontalAlignment="Left" Margin="73,0,0,0" Name="cmdprevious" VerticalAlignment="Top" Width="75" />
                    <Button Content="&gt;" Height="23" HorizontalAlignment="Left" Margin="146,0,0,0" Name="cmdnext" VerticalAlignment="Top" Width="75" />
                    <Button Content="&gt;|" Height="23" HorizontalAlignment="Right" Margin="0,0,91,0" Name="cmdLast" VerticalAlignment="Top" Width="75" />
                    <StackPanel Grid.Row="1" Height="303" HorizontalAlignment="Left" Margin="12,0,0,0" Name="StackPanel1" VerticalAlignment="Top" Width="369">
                            <Label Content="Naam" Height="28" Name="Label1" />
                            <Label Content="Voornaam" Height="28" Name="Label2" />
                            <Label Content="Euros op bankrekening" Height="28" Name="Label3" />
                    </StackPanel>
                    <StackPanel Grid.Column="1" Grid.Row="1" Height="300" HorizontalAlignment="Left" Name="StackPanel2" VerticalAlignment="Top" Width="385" DataContext="{StaticResource TestdatabaseViewSource}">
                            <TextBox Height="28" Name="txtnaam" Width="120" Text="{Binding Path=Naam, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationerror=true, ValidatesOnDataErrors=True}" IsManipulationEnabled="True" />
                            <TextBox Height="28" Name="txtvoornaam" Width="120" Text="{Binding Path=Voornaam, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationerror=true, ValidatesOnDataErrors=True}" IsManipulationEnabled="True" />
                            <TextBox Height="28" Name="txteurosopbankrekening" Width="120" Text="{Binding Path=Euro\'s op rekening, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationerror=true, ValidatesOnDataErrors=True}" IsManipulationEnabled="True" />
                    </StackPanel>
            </Grid>
    </Window>
Working...