By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,476 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,476 IT Pros & Developers. It's quick & easy.

424 Object Required Error

P: 5
Hi,

I have below code on Form_Load() , I get one time error when I open this form.

424 Object required, please help ASAP.


Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Private Sub Form_Load()
  3.  
  4. On Error GoTo ThisFormError
  5.  
  6. Dim cnt As ADODB.Connection
  7. Dim rst1 As ADODB.Recordset
  8. Dim stDB As String
  9. Dim stSQL1 As String
  10. Dim stConn As String
  11.  
  12.  
  13.  'Instantiate the ADO-objects.
  14. Set cnt = New ADODB.Connection
  15. Set rst1 = New ADODB.Recordset
  16.  
  17.  'Path to the database.
  18. stDB = "\\server23\abc\db.mdb"
  19.  
  20.  'Create the connectionstring.
  21. stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" _
  22. & "Data Source=" & stDB & ";Persist Security Info=False"
  23.  
  24.  
  25. cnt.Open (stConn)
  26. stSQL1 = "SELECT DISTINCT [table 1].[CH#] FROM [table1];"
  27.  
  28.  rst1.Open stSQL1, cnt, adOpenForwardOnly, adLockReadOnly, adCmdText
  29.  
  30.  
  31.         rst1.MoveFirst
  32.     With Me.Cmb_StoreNo
  33.  
  34.         Cmb_StoreNo.AddItem ("All ")
  35.  
  36.          Do While Not rst1.EOF
  37.          Cmb_StoreNo.AddItem rst1![CH#]
  38.          rst1.MoveNext
  39.  
  40.         Loop
  41.  End With
  42.  
  43. ThisFormErrorExit:
  44.         On Error Resume Next
  45.         rst.Close
  46.         Set rst = Nothing
  47.         cnt.Close
  48.         Set cnt = Nothing
  49. ThisFormError:
  50.  MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
  51.  Resume ThisFormErrorExit
  52. End Sub
Nov 7 '08 #1
Share this Question
Share on Google+
6 Replies


Expert Mod 2.5K+
P: 2,545
Hi. I have added code tags to your code, which give line numbers that make it easier to refer to specific lines.

It would have helped us if you had advised where in your code your object error is arising. Otherwise we are guessing. Informed guesswork, but still guessing.

I'd suggest looking at what follows the With statement in line 32. If you wish to refer to your combo properties you need to use the dot notation to do so - which you are not doing (lines 34 and 37). As you already specified the name of the combo in line 32 you do not need to repeat this. Instead the syntax for lines 34 and 37 should be

Expand|Select|Wrap|Line Numbers
  1. .AddItem ("All ") 
  2. .AddItem rst1![CH#]
As I said this is a guess at present - your error could be occurring before this point for all I know - but your syntax is definitely in error here.

-Stewart
Nov 7 '08 #2

ADezii
Expert 5K+
P: 8,597
In your code block, modify Lines 34, 37, 45, and 46, then add Line XX. The corrected code is posted below for your convenience:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command84_Click()
  2. On Error GoTo ThisFormError
  3.  Dim cnt As ADODB.Connection
  4. Dim rst1 As ADODB.Recordset
  5. Dim stDB As String
  6. Dim stSQL1 As String
  7. Dim stConn As String
  8.  
  9. 'Instantiate the ADO-objects.
  10. Set cnt = New ADODB.Connection
  11. Set rst1 = New ADODB.Recordset
  12.  
  13. 'Path to the database.
  14. 'stDB = "\\server23\abc\db.mdb"
  15. stDB = "C:\Ed Herbst\Ed New.mdb"
  16.  
  17.  'Create the connectionstring.
  18. stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" _
  19. & "Data Source=" & stDB & ";Persist Security Info=False"
  20.  
  21. cnt.Open (stConn)
  22. stSQL1 = "SELECT DISTINCT [table 1].[CH#] FROM [table1];"
  23.  
  24. rst1.Open stSQL1, cnt, adOpenForwardOnly, adLockReadOnly, adCmdText
  25.  
  26. rst1.MoveFirst
  27.  
  28. With Me.Cmb_StoreNo
  29.   .AddItem ("All ")
  30.     Do While Not rst1.EOF
  31.       Cmb_StoreNo.AddItem rst1![CH#]
  32.       rst1.MoveNext
  33.     Loop
  34. End With
  35.  
  36. ThisFormErrorExit:
  37.   On Error Resume Next
  38.   rst1.Close
  39.   Set rst1 = Nothing
  40.   cnt.Close
  41.   Set cnt = Nothing
  42.     Exit Sub
  43.  
  44. ThisFormError:
  45.   MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
  46.   Resume ThisFormErrorExit
  47. End Sub
Nov 8 '08 #3

ADezii
Expert 5K+
P: 8,597
In your code block, modify Lines 34, 37, 45, and 46, then add Line 42 in the corrected code block. The corrected code is posted below for your convenience, and the corrected lines in the revised code are: 29, 31, 38, and 39. There was never a rst Recordset in the 1st place, so you could never close it, ergo the Object Required Error. You must also Exit the Routine prior to the Msgbox Line (added Line 42).
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command84_Click()
  2. On Error GoTo ThisFormError
  3.  Dim cnt As ADODB.Connection
  4. Dim rst1 As ADODB.Recordset
  5. Dim stDB As String
  6. Dim stSQL1 As String
  7. Dim stConn As String
  8.  
  9. 'Instantiate the ADO-objects.
  10. Set cnt = New ADODB.Connection
  11. Set rst1 = New ADODB.Recordset
  12.  
  13. 'Path to the database.
  14. 'stDB = "\\server23\abc\db.mdb"
  15. stDB = "C:\Ed Herbst\Ed New.mdb"
  16.  
  17.  'Create the connectionstring.
  18. stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" _
  19. & "Data Source=" & stDB & ";Persist Security Info=False"
  20.  
  21. cnt.Open (stConn)
  22. stSQL1 = "SELECT DISTINCT [table 1].[CH#] FROM [table1];"
  23.  
  24. rst1.Open stSQL1, cnt, adOpenForwardOnly, adLockReadOnly, adCmdText
  25.  
  26. rst1.MoveFirst
  27.  
  28. With Me.Cmb_StoreNo
  29.   .AddItem ("All ")
  30.     Do While Not rst1.EOF
  31.       .AddItem rst1![CH#]
  32.       rst1.MoveNext
  33.     Loop
  34. End With
  35.  
  36. ThisFormErrorExit:
  37.   On Error Resume Next
  38.   rst1.Close
  39.   Set rst1 = Nothing
  40.   cnt.Close
  41.   Set cnt = Nothing
  42.     Exit Sub
  43.  
  44. ThisFormError:
  45.   MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
  46.   Resume ThisFormErrorExit
  47. End Sub
Nov 8 '08 #4

ADezii
Expert 5K+
P: 8,597
Hi. I have added code tags to your code, which give line numbers that make it easier to refer to specific lines.

It would have helped us if you had advised where in your code your object error is arising. Otherwise we are guessing. Informed guesswork, but still guessing.

I'd suggest looking at what follows the With statement in line 32. If you wish to refer to your combo properties you need to use the dot notation to do so - which you are not doing (lines 34 and 37). As you already specified the name of the combo in line 32 you do not need to repeat this. Instead the syntax for lines 34 and 37 should be

Expand|Select|Wrap|Line Numbers
  1. .AddItem ("All ") 
  2. .AddItem rst1![CH#]
As I said this is a guess at present - your error could be occurring before this point for all I know - but your syntax is definitely in error here.

-Stewart
Hello Stewart! The code segment that you are referring to, although not generally an accepted format, is still nonetheless fully functional. Just some useless information. Take care.
Nov 8 '08 #5

Expert Mod 2.5K+
P: 2,545
Thanks for the clarification ADezii.

@ Jatin32 I do wish you had let us know where in your code the original error was occurring, as it would have avoided looking at other lines in the code which although non-standard (and indeed not requiring a WITH statement to work) were not the source of the problem.

-S
Nov 8 '08 #6

P: 5
Thanks for the clarification ADezii.

@ Jatin32 I do wish you had let us know where in your code the original error was occurring, as it would have avoided looking at other lines in the code which although non-standard (and indeed not requiring a WITH statement to work) were not the source of the problem.

-S

This error did not give me any line number.
I have commented out 'On error line and it worked. I have not tried this posted code yet, but I will try it out and let you know.

Thanks a lot guys, appreciated.
Nov 10 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.