Question : Autogeneration of Alphanumeric in vb.net and inserting it to sql server

Hi,
1.    I want to autogenerate alphanumeric ID's in textbox1 which i have disabled so that user can't fill anything and cursor goes direct to textbox2.I want to insert fields(a/c id , name, address, phone, typofaccount, date, amount from vb.net 2005 express edition to sql server 2005 express edition. I haven't insert any record into sql, i want to insert a/c id like "ACO1" to "ACO9 " and then " AC10" to "AC99" and then "AC100" to "AC999" and so forth.I have written coding in form_load but it shows msg box till 10 and then shows ACO19 but i want it shows ACO1 and then if i press insert and again run form it should show AC02 and like wise.

2.     I also want to check that textbox2 should not remain empty if yes then there should be msgbox and focus should be again on textbox2 i have written coding for that but i have also written same coding for textbox3 but i stucked over that if i didn't write anything and goes to textbox3 then as on textbox3 i have written msg that it should not be empty so it just show msg box and when i enter it shows msg of textbox2 because that can't be empty.

3. By using Regular expression i want to check the pattern of phone no which should be of 10 digit numeric value only like 123-345-4343. but its showing error i copied that from some site but its not working i enter one digit and it shows msg and over that digit and msg, this goes on.


I am pasting code snippet, if you please check that and guide me that would be great.



Thanks

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
 
 
 
 
Public Class form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
 
 
        '("data source=TBPC25\SQLEXPRESS;Initial Catalog=RealEstate1;Integrated Security=True")
 
        Dim sqlconn As New SqlConnection("data source=TBPC25\SQLEXPRESS;Initial Catalog=banking;Inetegrated Security=True")
 
        Dim sqlcomm As New SqlCommand("insert into details values(acno=' " & TextBox1.Text & " ', name =' " & TextBox2.Text & " ', address = ' " & TextBox3.Text & " ',phone = " & TextBox4.Text & ",typeofac = ' " & ComboBox1.Text & " ',date = ' " & TextBox6.Text & " ', amount = " & TextBox7.Text & " ")
        'Dim da As New SqlDataAdapter
        sqlcomm.ExecuteNonQuery()
 
 
 
    End Sub
 
    Private Sub form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
       
    End Sub
 
    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Enabled = False
 
        Dim num As Integer
        Dim i As String = "AC01"
 
        num = 1
 
        While (num < 100)
 
            If num < 10 Then
                TextBox1.Text = i & num.ToString
                MsgBox(num)
 
            Else
 
 
                'TextBox1.Text = Microsoft.VisualBasic.Left(i, 2) & num.ToString
                'MsgBox(num)
            End If
            num += 1
 
        End While
 
 
        TextBox6.Enabled = False
 
        TextBox6.Text = Today()
        ComboBox1.Items.Add("Current A/C")
        ComboBox1.Items.Add("Saving A/C")
        ComboBox1.Items.Add("Fixed Deposit A/C")
 
 
    End Sub
 
    Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
        If TextBox2.Text = "" Then
            MsgBox("fill the name")
            TextBox2.Focus()
 
        End If
    End Sub
 
    Private Sub TextBox6_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.GotFocus
        
    End Sub
 
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 
        
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
 
    End Sub
 
    Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
        If TextBox3.Text = "" Then
            MsgBox("enter address")
            TextBox3.Focus()
 
        End If
    End Sub
 
    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        'e.Handled = TrapKey(Asc(e.KeyChar))
 
        If Not ValidatePhone(TextBox4.Text) Then
            MsgBox("Phone Numbers must be in a ###-###-#### format")
            TextBox4.Text = ""
            TextBox4.Focus()
        Else
            MsgBox("enter correct pattern")
        End If
 
    End Sub
 
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
 
    End Sub
    Private Function TrapKey(ByVal KCode As String) As Boolean
        '    If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
        'TrapKey = False
        'Else
        'TrapKey = True
        'End If
        'If Len(TextBox4.ToString) < 7 Or Len(TextBox4.ToString > 7) Then
        'MsgBox("length should be 7 ")
 
        'End If
    End Function
 
    Public Function ValidatePhone(ByVal num As String) As Boolean
        'create our regular expression pattern
        Dim pattern As String = "^\d{3}-\d{3}-\d{4}$"
        'create our regular expression object
        Dim check As New Regex(pattern)
        Dim valid As Boolean = False
        'Make sure a phone number was provided
        If Not String.IsNullOrEmpty(num) Then
            valid = check.IsMatch(num)
        Else
            valid = False
        End If
        Return valid
    End Function
 
 
    
End Class

Answer : Autogeneration of Alphanumeric in vb.net and inserting it to sql server

Glad I could help you.
Don't forget to close this question so that the cleanup volunteers or moderators have less work !
Random Solutions  
 
programming4us programming4us