Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Public mask As Integer
- Public Sub New()
- ' This call is required by the designer.
- InitializeComponent()
- ' Add any initialization after the InitializeComponent() call.
- ' set the Tag value for each textbox (this is the power of 2)
- ' it is the mask that is added/subtracted to a total (mask) if there text is text in the textbox or not
- txtRef.Tag = 1
- txtQty.Tag = 2
- txtYear.Tag = 4
- txtAuthor.Tag = 8
- txtPrice.Tag = 16
- txtBook.Tag = 32
- txtPub.Tag = 64
- 'etc
- btnAddRecord.Enabled = False
- ClearTextBoxes(Me)
- End Sub
- Private Sub SaveRecord()
- Dim con As New OleDb.OleDbConnection
- Dim cmdinfo As New OleDb.OleDbCommand
- Dim dbProvider As String
- Dim dbSource As String
- dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0; "
- dbSource = "Data Source = <path to your MDB ie C:\Users\Documents\Database2.mdb>;"
- con.ConnectionString = dbProvider & dbSource
- ' OK this is the wrong way to do it BUT it keeps it nice and simple for a demo
- ' really you should create this SQL statement dynamically in a different function and pass it into this function
- With cmdinfo
- .CommandType = CommandType.Text
- .CommandText = "SELECT * FROM MECHATRONICS"
- .CommandText = "INSERT INTO MECHATRONICS (`REFNO`, `QTY`, `BOOKTITLE`, `YEAR`, `AUTHOR`, `PRICE`, `PUBLISHER`) values (@REFNO,@QTY,@BOOKTITLE,@YEAR,@AUTHOR,@PRICE,@PUBLISHER)"
- .CommandType = CommandType.Text ' You missed this line
- .Parameters.AddWithValue("@xid", txtRef.Text)
- .Parameters.AddWithValue("@m_id", txtQty.Text)
- .Parameters.AddWithValue("@imodel", txtYear.Text)
- .Parameters.AddWithValue("@icolor", txtAuthor.Text)
- .Parameters.AddWithValue("@ch_id", txtPrice.Text)
- .Parameters.AddWithValue("@pt_num", txtBook.Text)
- .Parameters.AddWithValue("@icode", txtPub.Text)
- End With
- cmdinfo.Connection = con
- cmdinfo.Connection.Open()
- cmdinfo.ExecuteNonQuery()
- con.Close()
- End Sub
- Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
- ' OK this uses bit masking to validate that there is text in the textbox...
- ' it might seem like a lot of touble to go to but it works well.
- ' OK so what we are doing in the next few lines of code is 'Anding' the value of 'mask'
- ' with the tag value of the current textbox
- ' this will tell us if we have previously verified this textbox and confirmed that it contained text
- ' now, (because the user has modified the text in this textbox) we have to verify that it still contains
- ' some text, if not then we subtract the tag value from mask, if it does then we leave the value of mask alone…
- ' example
- 'mask value of 20 and’ed with tag 4 = 4 (we have previously verified textbox 3)
- 'mask value of 20 and’ed with tag 16 = 16 (we have previously verified textbox text 5)
- 'mask value of 20 and’ed with tag 1 = 0 (we have NOT previously verified textbox text 1)
- If (mask And DirectCast(sender, System.Windows.Forms.TextBox).Tag) = DirectCast(sender, System.Windows.Forms.TextBox).Tag Then ' Mask already added
- If DirectCast(sender, System.Windows.Forms.TextBox).Text = String.Empty Then ' if there is no text in this textbox
- mask -= DirectCast(sender, System.Windows.Forms.TextBox).Tag ' subtract the mask
- End If
- ElseIf DirectCast(sender, System.Windows.Forms.TextBox).Text <> String.Empty Then ' mask is not added
- mask += DirectCast(sender, System.Windows.Forms.TextBox).Tag
- End If
- btnAddRecord.Enabled = mask = 127 ' for 6 textboxes set this value to 63 for 7 textboxes set it to 127 and so on
- End Sub
- Private Sub ClearTextBoxes(form1 As Form1)
- ' this function loops round all of the controls on this form and clears all the TextBoxes
- For Each ctrl As Control In Me.Controls
- If TypeOf ctrl Is TextBox Then
- CType(ctrl, TextBox).Text = "" ' set the initialy selected item to -1 (nothing)
- ' the next line is adding a delegate to each TextBox TextChanged event…
- ' it is basicly saying, when the text changes in the (any) textbox then fire TextBox_TextChanged event
- AddHandler CType(ctrl, TextBox).TextChanged, AddressOf Me.TextBox_TextChanged
- End If
- Next ctrl
- End Sub
- Private Sub btnAddRecord_Click(sender As Object, e As EventArgs) Handles btnAddRecord.Click
- ' 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....
- SaveRecord()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement