January 6, 2013

Visual basic Examples-Adding items from listbox to database,Validating user name and password ,Primary Key at Runtime

Adding items from listbox to database
First you need to write code for connection and recordset opening then you can set following code in your program

 for i = 0 to list1.listcount -1  
 if list1.selected(i) = true then  
 rs!fieldname = list1.list(i)  
 next i  

Validating user name and password from the database using visual basic

 Set con = CreateObject("adodb.connection")  
 con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Persist Security Info=False"  
 con.Execute ("select Username, Password from table1 where Username='" & UID & "'")  
 If Not con.Execute("select * from table1 where [username]='" & txtUsername.Text & _  
 "' AND [password]='" & txtPassword.Text & "'").EOF Then  
 MsgBox "Login Successfull", vbInformation, "Success!"  
 MsgBox "Login Failed-Enter UserID and Password Correctly", vbCritical, "Failure!"  
 txtUsername.Text = ""  
 txtPassword.Text = ""  
 End If  
 End Sub  
Finding primary key of access table during runtime in visual basic

 Dim cat As Object, tbl As Object  
 Set cat = CreateObject("ADOX.Catalog")  
 cat.ActiveConnection = con  
 Set tbl = cat.Tables("fat")  
 For Each Key In tbl.Keys  
 If Key.Name = "PrimaryKey" Then  
 For Each clm In Key.Columns  
 Print clm.Name  
 Exit Sub  
 End If