Advertisement
NAK

AddMDBRecord

NAK
Apr 29th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.98 KB | None | 0 0
  1. Public Class Form1
  2.     Public mask As Integer
  3.  
  4.     Public Sub New()
  5.  
  6.         ' This call is required by the designer.
  7.         InitializeComponent()
  8.  
  9.         ' Add any initialization after the InitializeComponent() call.
  10.         ' set the Tag value for each textbox (this is the power of 2)
  11.         ' it is the mask that is added/subtracted to a total (mask) if there text is text in the textbox or not
  12.         txtRef.Tag = 1
  13.         txtQty.Tag = 2
  14.         txtYear.Tag = 4
  15.         txtAuthor.Tag = 8
  16.         txtPrice.Tag = 16
  17.         txtBook.Tag = 32
  18.         txtPub.Tag = 64
  19.         'etc
  20.         btnAddRecord.Enabled = False
  21.         ClearTextBoxes(Me)
  22.     End Sub
  23.  
  24.     Private Sub SaveRecord()
  25.  
  26.         Dim con As New OleDb.OleDbConnection
  27.         Dim cmdinfo As New OleDb.OleDbCommand
  28.         Dim dbProvider As String
  29.         Dim dbSource As String
  30.  
  31.         dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0; "
  32.         dbSource = "Data Source = <path to your MDB ie C:\Users\Documents\Database2.mdb>;"
  33.  
  34.         con.ConnectionString = dbProvider & dbSource
  35.  
  36.         ' OK this is the wrong way to do it BUT it keeps it nice and simple for a demo
  37.         ' really you should create this SQL statement dynamically in a different function and pass it into this function
  38.         With cmdinfo
  39.             .CommandType = CommandType.Text
  40.             .CommandText = "SELECT * FROM MECHATRONICS"
  41.             .CommandText = "INSERT INTO MECHATRONICS (`REFNO`, `QTY`, `BOOKTITLE`, `YEAR`, `AUTHOR`, `PRICE`, `PUBLISHER`) values (@REFNO,@QTY,@BOOKTITLE,@YEAR,@AUTHOR,@PRICE,@PUBLISHER)"
  42.  
  43.             .CommandType = CommandType.Text ' You missed this line
  44.             .Parameters.AddWithValue("@xid", txtRef.Text)
  45.             .Parameters.AddWithValue("@m_id", txtQty.Text)
  46.             .Parameters.AddWithValue("@imodel", txtYear.Text)
  47.             .Parameters.AddWithValue("@icolor", txtAuthor.Text)
  48.             .Parameters.AddWithValue("@ch_id", txtPrice.Text)
  49.             .Parameters.AddWithValue("@pt_num", txtBook.Text)
  50.             .Parameters.AddWithValue("@icode", txtPub.Text)
  51.         End With
  52.  
  53.         cmdinfo.Connection = con
  54.         cmdinfo.Connection.Open()
  55.         cmdinfo.ExecuteNonQuery()
  56.  
  57.         con.Close()
  58.  
  59.     End Sub
  60.  
  61.     Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
  62.  
  63.         ' OK this uses bit masking to validate that there is text in the textbox...
  64.         ' it might seem like a lot of touble to go to but it works well.
  65.  
  66.         ' OK so what we are doing in the next few lines of code is 'Anding' the value of 'mask'
  67.         ' with the tag value of the current textbox
  68.         ' this will tell us if we have previously verified this textbox and confirmed that it contained text
  69.         ' now, (because the user has modified the text in this textbox) we have to verify that it still contains
  70.         ' some text, if not then we subtract the tag value from  mask, if it does then we leave the value of mask  alone…
  71.         ' example
  72.         'mask value of 20 and’ed with tag 4 = 4 (we have previously verified textbox 3)
  73.         'mask value of 20 and’ed with tag 16 = 16 (we have previously verified textbox text 5)
  74.         'mask value of 20 and’ed with tag 1 = 0 (we have NOT previously verified textbox text 1)
  75.  
  76.         If (mask And DirectCast(sender, System.Windows.Forms.TextBox).Tag) = DirectCast(sender, System.Windows.Forms.TextBox).Tag Then ' Mask already added
  77.             If DirectCast(sender, System.Windows.Forms.TextBox).Text = String.Empty Then ' if there is no text in this textbox
  78.                 mask -= DirectCast(sender, System.Windows.Forms.TextBox).Tag ' subtract the mask
  79.             End If
  80.         ElseIf DirectCast(sender, System.Windows.Forms.TextBox).Text <> String.Empty Then ' mask is not added
  81.             mask += DirectCast(sender, System.Windows.Forms.TextBox).Tag
  82.         End If
  83.  
  84.         btnAddRecord.Enabled = mask = 127 ' for 6 textboxes set this value to 63 for 7 textboxes set it to 127 and so on
  85.  
  86.     End Sub
  87.  
  88.     Private Sub ClearTextBoxes(form1 As Form1)
  89.  
  90.         ' this function loops round all of the controls on this form and clears all the TextBoxes
  91.         For Each ctrl As Control In Me.Controls
  92.             If TypeOf ctrl Is TextBox Then
  93.                 CType(ctrl, TextBox).Text = "" ' set the initialy selected item to -1 (nothing)
  94.                 ' the next line is adding a delegate to each TextBox TextChanged event…
  95.                 ' it is basicly saying, when the text changes in the (any) textbox then fire TextBox_TextChanged event
  96.                 AddHandler CType(ctrl, TextBox).TextChanged, AddressOf Me.TextBox_TextChanged
  97.             End If
  98.         Next ctrl
  99.  
  100.     End Sub
  101.  
  102.     Private Sub btnAddRecord_Click(sender As Object, e As EventArgs) Handles btnAddRecord.Click
  103.         ' this just calls the save record event, notice that this button is desabled UNTIL you have filled in every terxt box....even if you fill them in randomly or remove text....
  104.         SaveRecord()
  105.     End Sub
  106.  
  107. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement