Click here to Skip to main content
15,881,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Facts: I am trying to connect VB6 login form to MS access database. The condition is, when i login in the vb login form it supposed to check whether the username and password is correct or not. if correct another form supposed to open which name is mainmenu.

Here the error is unrecognized database format.i don't know why it showing this error because i put the correct file and file extension. Please help me how can i work on this condition. do you have any other idea to connect and check password and username.

VB
'CHECKING Username and password
Public Function validuser(Username As String, password As String)

Dim db As String
Dim Cmd As String
Dim sql As String
Dim Cn As ADODB.Connection
Dim rs As ADODB.Recordset

db = App.Path & "\nfl.accdb"
Cmd = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db & ""

Set Cn = New ADODB.Connection

With Cn
    .ConnectionString = Cmd
    .Open
End With

Set rs = New ADODB.Recordset


sql = "Select * From [Security] where Username LIKE '" & Username & "' and Password LIKE '" & password & "'"
rs.Open sql, Cn, adOpenForwardOnly, adLockReadOnly

 
 If Not rs.EOF Then
 validuser = True
 Else
 validuser = False
 End If
 
 rs.Close
 Set rs = Nothing

 Cn.Close
 Set Cn = Nothing
End Function
Posted
Updated 30-Oct-14 21:38pm
v2
Comments
Richard Deeming 3-Nov-14 17:34pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Also, you're storing passwords in plain text. That is a very bad idea. You should only ever store a salted hash of the user's password, using multiple rounds of a strong hashing algorithm.

You cannot use the earlier versions of JET to connect to newer version of Access dbs. You need to use the ACE OLEDB 12.

CSS
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;
 
Share this answer
 
v2
Comments
Lamichhane 31-Oct-14 1:04am    
still error is " expected end of statement" in the line of persiste security info= false; what may be the solution pls
Maciej Los 31-Oct-14 3:40am    
+5
AnvilRanger answer is very good. In addition to it, i'd suggest to start here:
Using ADO with Microsoft Visual Basic[^]
ADO Code Examples in Visual Basic[^]

Proper connection string, you'll find here: http://www.connectionstrings.com/access/[^]

By the way, such as query:
sql = "Select * From [Security] where Username LIKE '" & Username & "' and Password LIKE '" & password & "'"
is wrong!
Why? Please, have a look here: Like Operator (Access)[^]

I'd suggest to change above query to query, which uses parameters[^]:
SQL
PARAMETERS [uname] CHAR, [pass] CHAR;
SELECT [UserName], [Password]
FROM [Security]
WHERE [UserName] = [uname] and [Password] = [pass]


How to use it?
Declare ADODB.Command[^] and Create Parameter (ADO)[^] to add it to the command's Parameters Collection (ADO)[^]. Finally, call Execute Method (ADO Command)[^] to create ADODB.recordset.

There are some restrictions for MS Access database, like list of reserved words[^], which can't be used.
 
Share this answer
 
Comments
DamithSL 2-Nov-14 22:08pm    
5wd !
Maciej Los 3-Nov-14 1:44am    
Thank you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900