Suppose when the login textbox left blank and we press enter, it prompts out a message. But for my case, it also prompts out this error code.
Run-time error 91:
Object variable or with block variable not set.
Below is my code for that part that i did.
Private Sub txtLogin_KeyDown(KeyCode As Integer, Shift As Integer)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
If KeyCode = 13 Then
If Trim(txtLogin.Text = "") Then
MsgBox "Please Login using login ID!", vbOKOnly, "Field Required!"
txtLogin.SetFocus
Else
Dim oWorkbook As Workbook
Set oWorkbook = Excel.Workbooks.Open(FileName:="" & Trim(GetINISetting("Excel_Path", "DirectoryPath", App.Path & "\SETTINGS.INI")) & "LoginTable1.xlsx", Password:="" & Trim(GetINISetting("Pwd", "Pass", App.Path & "\SETTINGS.INI")) & "")
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Trim(GetINISetting("Excel_Path", "DirectoryPath", App.Path & "\SETTINGS.INI")) & "LoginTable1.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""")
If rs.State = adStateOpen Then rs.Close
rs.Open ("select * from [sheet1$] where Login_ID ='" & txtLogin.Text & "'"), cn, adOpenStatic, adLockReadOnly
If rs.RecordCount > 0 Then
txtLogin.Text = rs!Login_ID
Login.Visible = False
FormMain1.Show
Else
MsgBox "Incorrect Login ID.", vbOKOnly, "Message"
txtLogin.Text = ""
txtLogin.SetFocus
End If
End If
oWorkbook.Close
Set oWorkbook = Nothing
End If
End Sub
The mistake it shows is at
oWorkbook.Close that line.
Any help would be much appreciated. Thanks.
This post has been edited by tbzserge: 28 November 2012 - 01:00 AM

New Topic/Question
Reply




MultiQuote




|