Link to home
Start Free TrialLog in
Avatar of pg1533
pg1533

asked on

Object Required Error

Hi,

I have attached an excel 'Forecast-Tool' and it has the form named 'LoginFrm' created. When I open this excel the 'LoginFrm' would popup to ask for login credentials. I will give user name as 'Prashanth' and the password is 'X'. Once, I click on Login button then I get the error as 'Object Required' This workbook will be connected to 'ForecastToolDatabase.mdb' file which is also attached.

I am not able to identify where is the problem. It would be of great help if you could do the needful. Thanks for your time and support.

Regards,
Prashanth
Forecast-Tool.xlsm
ForecastToolDatabase.mdb
Avatar of [ fanpages ]
[ fanpages ]

Hi Prashanth,

I changed the modLoginForm code module to read:

Option Explicit                                                             ' *** Added

Public clsADODB                                         As New clsADODB     ' *** Added
Sub btn_Login_Click()
On Error GoTo ErrorHandler
    Dim strUsername As String, strPassword As String
    Dim strQuery As String
    Dim objRSet                                         As Recordset        ' *** Added
    
    If Trim(LoginFrm.LoginFrmUserText.Text) = "" Or Trim(LoginFrm.LoginFrmPassTxt.Text) = "" Then
        MsgBox "Please fill all login credentials", vbExclamation, "Login Fail"
    Else
        'WSLogin.lblError.Caption = ""
        Call clsADODB.ConnecttoDatabase
        Set objRSet = New Recordset                                         ' *** Added
        objRSet.CursorType = adOpenStatic
        objRSet.Open "SELECT UserDetails.UserName, UserDetails.FullName, UserDetails.UserCat, UserDetails.Password FROM UserDetails WHERE (((UserDetails.UserName)='" & Trim(LoginFrm.LoginFrmUserText.Text) & "') AND ((tblUser.Password)='" & Trim(LoginFrm.LoginFrmPassTxt) & "'));", clsADODB.objCon
        If objRSet.RecordCount <> 0 Then
            If Trim(LoginFrm.LoginFrmUserText.Text) = objRSet.Fields("UserName") _
            And Trim(LoginFrm.LoginFrmPassTxt) = objRSet.Fields("Password") Then
                
                MsgBox "Success", vbInformation
                
            Else
            
                MsgBox "Fail", vbCritical
            
            End If
        
        Else
                
            MsgBox "Fail", vbCritical
            
        End If

    End If
    
If clsADODB.objCon.State = adStateOpen Then clsADODB.objCon.Close
Exit Sub
    
ErrorHandler:
        MsgBox Err.Description, vbInformation, "Error Information"
End Sub

Open in new window



I also changed the clsADODB.ConnecttoDatabase() subroutine.

The previous line:
strConn = "ODBC;DBQ=" & ActiveWorkbook.Path & "\ForecastToolDatabase.mdb;Driver={Microsoft Access Driver (*.mdb)}"

Open in new window


Now reads:
strConn = "ODBC;DBQ=" & ThisWorkbook.Path & "\ForecastToolDatabase.mdb;Driver={Microsoft Access Driver (*.mdb)}"

Open in new window


(ActiveWorkbook becomes ThisWorkbook)

The code now progresses to the SQL statement within the modLoginForm.btn_Login_Click() subroutine:

objRSet.Open "SELECT UserDetails.UserName, UserDetails.FullName, UserDetails.UserCat, UserDetails.Password FROM UserDetails WHERE (((UserDetails.UserName)='" & Trim(LoginFrm.LoginFrmUserText.Text) & "') AND ((tblUser.Password)='" & Trim(LoginFrm.LoginFrmPassTxt) & "'));", clsADODB.objCon

Open in new window


However, this fails with the error message:
"[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2."

Do you need this problem resolving or are you capable of looking at this yourself?

BFN,

fp.
PS. The reason for the change that was necessary within the clsADODB.ConnecttoDatabase() subroutine was due to the incomplete advice you were offered here:

[ https://www.experts-exchange.com/questions/28144627/Form-in-Excel.html?anchorAnswerId=39212381#a39212381 ]
Avatar of pg1533

ASKER

It would be of great help and I really appreciate, if you could resolve this problem also. Thank you so much for your time, patience and support.

Regards,
Prashanth
Hi again,

Change this line:
objRSet.Open "SELECT UserDetails.UserName, UserDetails.FullName, UserDetails.UserCat, UserDetails.Password FROM UserDetails WHERE (((UserDetails.UserName)='" & Trim(LoginFrm.LoginFrmUserText.Text) & "') AND ((tblUser.Password)='" & Trim(LoginFrm.LoginFrmPassTxt) & "'));", clsADODB.objCon

Open in new window


To read like this:
objRSet.Open "SELECT UserName, FullName, User_Cat, Password FROM UserDetails WHERE UserName='" & Trim(LoginFrm.LoginFrmUserText.Text) & "' AND Password='" & Trim(LoginFrm.LoginFrmPassTxt) & "'", clsADODB.objCon

Open in new window


You should then see the "Success" (information) MessageBox.

BFN,

fp.
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pg1533

ASKER

Hi Fanpages,

You are Genius. Thank you so much for all your support and time. I would continue to work on the project. I would definitely raise another question if I get any doubt.

Once again thank you so much. Take care.

Regards,
Prashanth
Avatar of pg1533

ASKER

Thank you
You are very welcome, Prashanth.

If I spot your further question(s), & nobody else is assisting you, I will try to contribute if I am able to.

BFN,

fp.