By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,064 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,064 IT Pros & Developers. It's quick & easy.

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

P: 1
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

Expand|Select|Wrap|Line Numbers
  1. Class MainWindow
  2.  
  3.         Private testdatabaseData As New testdatabaseDataSet
  4.         Private tatestdatabase As New testdatabaseDataSetTableAdapters.testdatabaseTableAdapter
  5.         Private taManager As New testdatabaseDataSetTableAdapters.TableAdapterManager
  6.         Private View As CollectionView
  7.  
  8.         Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
  9.                 Me.tatestdatabase.Fill(Me.testdatabaseData.testdatabase)
  10.  
  11.                 Me.DataContext = Me.testdatabaseData.testdatabase
  12.  
  13.                 Me.taManager.testdatabaseTableAdapter = tatestdatabase
  14.  
  15.                 Me.DataContext = Me.testdatabaseData.testdatabase
  16.  
  17.                 Me.View = CType(CollectionViewSource.GetDefaultView(Me.testdatabaseData.testdatabase), CollectionView)
  18.  
  19.                 Dim TestdatabaseDataSet As WpfDataBase.testdatabaseDataSet = CType(Me.FindResource("TestdatabaseDataSet"), WpfDataBase.testdatabaseDataSet)
  20.                 'Load data into the table testdatabase. You can modify this code as needed.
  21.                 Dim TestdatabaseDataSettestdatabaseTableAdapter As WpfDataBase.testdatabaseDataSetTableAdapters.testdatabaseTableAdapter = New WpfDataBase.testdatabaseDataSetTableAdapters.testdatabaseTableAdapter()
  22.                 TestdatabaseDataSettestdatabaseTableAdapter.Fill(TestdatabaseDataSet.testdatabase)
  23.                 Dim TestdatabaseViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("TestdatabaseViewSource"), System.Windows.Data.CollectionViewSource)
  24.                 TestdatabaseViewSource.View.MoveCurrentToFirst()
  25.         End Sub
  26.  
  27.         Private Sub cmdfirst_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdfirst.Click
  28.                 Me.View.MoveCurrentToFirst()
  29.  
  30.         End Sub
  31.  
  32.         Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdprevious.Click
  33.                 If Me.View.CurrentPosition > 0 Then
  34.                         Me.View.MoveCurrentToPrevious()
  35.                 End If
  36.  
  37.         End Sub
  38.  
  39.         Private Sub cmdnext_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdnext.Click
  40.                 If Me.View.CurrentPosition < Me.View.Count - 1 Then
  41.                         Me.View.MoveCurrentToNext()
  42.                 End If
  43.  
  44.         End Sub
  45.  
  46.         Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdLast.Click
  47.                 Me.View.MoveCurrentToLast()
  48.         End Sub
  49.  
  50.         Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdDelete.Click
  51.                 If Me.View.CurrentPosition > -1 Then
  52.                         Dim row = CType(Me.View.CurrentItem, System.Data.DataRowView).Row
  53.                         row.Delete()
  54.                 End If
  55.         End Sub
  56.  
  57.         Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdAdd.Click
  58.                 If Not Me.testdatabaseData.HasErrors Then
  59.                         Dim row = Me.testdatabaseData.testdatabase.NewtestdatabaseRow
  60.                         row.Naam = ""
  61.                         Me.testdatabaseData.testdatabase.AddtestdatabaseRow(row)
  62.                         Me.View.MoveCurrentToLast()
  63.                 End If
  64.         End Sub
  65.  
  66.         Private Sub cmdRevert_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdRevert.Click
  67.                 If Me.testdatabaseData.HasChanges Then
  68.                         If MessageBox.Show("Are you sure you want to lose all your changes?", Me.Title, MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
  69.                                 Me.testdatabaseData.RejectChanges()
  70.                         End If
  71.                 End If
  72.  
  73.         End Sub
  74.  
  75.         Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdSave.Click
  76.                 Try
  77.                         If Me.testdatabaseData.HasErrors Then
  78.                                 MsgBox("Please correct the errors first")
  79.                         Else
  80.                                 If Me.testdatabaseData.HasChanges Then
  81.                                         If Me.taManager.UpdateAll(Me.testdatabaseData) > 0 Then
  82.                                                 MsgBox("Saved.")
  83.                                         End If
  84.                                 End If
  85.                         End If
  86.  
  87.                 Catch ex As Exception
  88.                         MsgBox(ex.ToString)
  89.  
  90.                 End Try
  91.  
  92.         End Sub
  93.  
  94. End Class
  95.  
  96. Partial Class testdatabaseDataSet
  97.         Partial Class testdatabaseDataTable
  98.  
  99.                 Private Sub CheckNaam(ByVal row As testdatabaseRow)
  100.                         If row.IsNull("Naam") OrElse row.Naam = "" Then
  101.                                 row.SetColumnError(Me.NaamColumn, "Please enter a naam")
  102.  
  103.                         Else
  104.                                 row.SetColumnError(Me.NaamColumn, "")
  105.                         End If
  106.                 End Sub
  107.  
  108.                 Private Sub testdatabaseDataTable_Columnchanged(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanged
  109.                         If e.Column Is Me.NaamColumn Then
  110.                                 Me.CheckNaam(CType(e.Row, testdatabaseRow))
  111.                         End If
  112.  
  113.                 End Sub
  114.  
  115.                 Private Sub testdatabaseDataTable_TableNewRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) Handles Me.TableNewRow
  116.                         Me.CheckNaam(CType(e.Row, testdatabaseRow))
  117.                 End Sub
  118.  
  119.  
  120.  
  121.         End Class
  122.  
  123. End Class
Expand|Select|Wrap|Line Numbers
  1. Class="MainWindow"
  2.         xmlns="http://schemas.micro...l/presentation"
  3.         xmlns:x="http://schemas.micro...infx/2006/xaml"
  4.         Title="MainWindow" Height="406" Width="814" xmlns:my="clr-namespace:WpfDataBase">
  5.         <window.Resources>
  6.                 <my:testdatabaseDataSet x:Key="TestdatabaseDataSet" />
  7.                 <CollectionViewSource x:Key="TestdatabaseViewSource" Source="{Binding Path=testdatabase, Source={StaticResource TestdatabaseDataSet}}" />
  8.         </window.Resources>
  9.         <Grid Height="369" Width="770">
  10.                 <Grid.RowDefinitions>
  11.                         <RowDefinition Height="66*" />
  12.                         <RowDefinition Height="303*" />
  13.                 </Grid.RowDefinitions>
  14.                 <Grid.ColumnDefinitions>
  15.                         <ColumnDefinition Width="200*" />
  16.                         <ColumnDefinition Width="200*" />
  17.                 </Grid.ColumnDefinitions>
  18.                 <Button Content="Save" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="237,0,0,0" Name="cmdSave" VerticalAlignment="Top" Width="75" />
  19.                 <Button Content="Revert" HorizontalAlignment="Right" Margin="0,0,0,43" Name="cmdRevert" Width="75" Grid.Column="1" />
  20.                 <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="164,0,0,0" Name="cmdAdd" VerticalAlignment="Top" Width="75" Grid.Column="1" />
  21.                 <Button Content="Delete" HorizontalAlignment="Left" Margin="112,0,0,43" Name="cmdDelete" Width="55" Grid.Column="1" BorderBrush="#FFFA0000" BorderThickness="3" />
  22.                 <Button Content="|&lt;" Height="23" HorizontalAlignment="Left" Name="cmdfirst" VerticalAlignment="Top" Width="75" />
  23.                 <Button Content="&lt;" Height="23" HorizontalAlignment="Left" Margin="73,0,0,0" Name="cmdprevious" VerticalAlignment="Top" Width="75" />
  24.                 <Button Content="&gt;" Height="23" HorizontalAlignment="Left" Margin="146,0,0,0" Name="cmdnext" VerticalAlignment="Top" Width="75" />
  25.                 <Button Content="&gt;|" Height="23" HorizontalAlignment="Right" Margin="0,0,91,0" Name="cmdLast" VerticalAlignment="Top" Width="75" />
  26.                 <StackPanel Grid.Row="1" Height="303" HorizontalAlignment="Left" Margin="12,0,0,0" Name="StackPanel1" VerticalAlignment="Top" Width="369">
  27.                         <Label Content="Naam" Height="28" Name="Label1" />
  28.                         <Label Content="Voornaam" Height="28" Name="Label2" />
  29.                         <Label Content="Euros op bankrekening" Height="28" Name="Label3" />
  30.                 </StackPanel>
  31.                 <StackPanel Grid.Column="1" Grid.Row="1" Height="300" HorizontalAlignment="Left" Name="StackPanel2" VerticalAlignment="Top" Width="385" DataContext="{StaticResource TestdatabaseViewSource}">
  32.                         <TextBox Height="28" Name="txtnaam" Width="120" Text="{Binding Path=Naam, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationerror=true, ValidatesOnDataErrors=True}" IsManipulationEnabled="True" />
  33.                         <TextBox Height="28" Name="txtvoornaam" Width="120" Text="{Binding Path=Voornaam, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationerror=true, ValidatesOnDataErrors=True}" IsManipulationEnabled="True" />
  34.                         <TextBox Height="28" Name="txteurosopbankrekening" Width="120" Text="{Binding Path=Euro\'s op rekening, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationerror=true, ValidatesOnDataErrors=True}" IsManipulationEnabled="True" />
  35.                 </StackPanel>
  36.         </Grid>
  37. </Window>
Sep 24 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.