Results 1 to 16 of 16

Thread: [RESOLVED] Object Variable or Block Variable not set Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    [RESOLVED] Object Variable or Block Variable not set Error

    Hello... I'm trying to write a simple program for a class to pull data from a table and write it to a random file.. or text file.. but i keep getting that stupid error message "Object Variable or Block Variable not set" Here is my Code..
    VB Code:
    1. Option Explicit
    2. Private Type Customer
    3.     CNumber As String
    4.     FName As String
    5.     LName As String
    6.     Address As String
    7.     City As String
    8.     State As String
    9.     Zip As Integer
    10. End Type
    11.  
    12.  
    13. Private Sub Form_Load()
    14.  
    15. Dim x As Integer
    16.  
    17. Dim Cust(4) As Customer
    18.     Do While Not DataC1.Recordset.EOF
    19.         Cust(x).FName = DataC1.Recordset!FName
    20.         Cust(x).LName = DataC1.Recordset!LName
    21.         Cust(x).Address = DataC1.Recordset!Address
    22.         Cust(x).City = DataC1.Recordset!City
    23.         Cust(x).State = DataC1.Recordset!State
    24.         Cust(x).Zip = DataC1.Recordset!Zip
    25.         Cust(x).CNumber = DataC1.Recordset!CNumber
    26.         DataC1.Recordset.MoveNext
    27.         x = x + 1
    28.     Loop
    29.  
    30. Put #1, , Cust
    31.    
    32. End Sub
    Last edited by VB.Newb; Feb 13th, 2006 at 02:09 PM.

  2. #2
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: Object Variable or Block Variable not set Error

    ok this may sound crazy, but check your refrense files, and move some of them up and down. When trying to use directx7 and 8 in the same program I got this alot. So just give it a whirl. It might just work...
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    Quote Originally Posted by VB.Newb
    Hello... I'm trying to write a simple program for a class to pull data from a table and write it to a random file.. or text file.. but i keep getting that stupid error message "Object Variable or Block Variable not set" Here is my Code..
    VB Code:
    1. Option Explicit
    2. Private Type Customer
    3.     CNumber As String
    4.     FName As String
    5.     LName As String
    6.     Address As String
    7.     City As String
    8.     State As String
    9.     Zip As Integer
    10. End Type
    11.  
    12.  
    13. Private Sub Form_Load()
    14.  
    15. Dim x As Integer
    16.  
    17. Dim Cust(4) As Customer
    18.     Do While Not DataC1.Recordset.EOF
    19.         Cust(x).FName = DataC1.Recordset!FName
    20.         Cust(x).LName = DataC1.Recordset!LName
    21.         Cust(x).Address = DataC1.Recordset!Address
    22.         Cust(x).City = DataC1.Recordset!City
    23.         Cust(x).State = DataC1.Recordset!State
    24.         Cust(x).Zip = DataC1.Recordset!Zip
    25.         Cust(x).CNumber = DataC1.Recordset!CNumber
    26.         DataC1.Recordset.MoveNext
    27.         x = x + 1
    28.     Loop
    29.  
    30. Put #1, , Cust
    31.    
    32. End Sub
    Welcome to the forums...

    Just so you know, when you copy and paste Visual Basic code, encapsulate the code in vbcode tags, like this
    [vbcode]
    .... your Visual Basic code here ....
    [/vbcode]
    It will make your code much easier to read
    Last edited by CVMichael; Feb 9th, 2006 at 05:26 PM.

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    UPS... stupid me, I just noticed, that Customer is a type NOT class....
    I edited the code as it was, in the previous post...

    Anyways...
    In that case, object DataC1 is probably not set to an actual instance of the object

    How many records does the Recordset have ?
    If you have more than 5, then you will get an error at this line:
    VB Code:
    1. Dim Cust(4) As Customer
    You should make a dinamic array, and redim by the number of records, like this:
    VB Code:
    1. Dim Cust() As Customer
    2. ReDim Cust(DataC1.Recordset.RecordCount)
    Last edited by CVMichael; Feb 9th, 2006 at 05:30 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Re: Object Variable or Block Variable not set Error

    it's just a simple program.. and the database table only has 2 records in it.. and for the purposes of this wont get any bigger..I tried using the
    VB Code:
    1. Dim Cust() As Customer
    2. ReDim Cust(DataC1.Recordset.RecordCount)
    but i still got the same error

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    OK, then we don't have enough information from you, so that we can find out the problem.

    At what line of code the VB stops ?
    When you get the error, VB should prompt you to "Debug", click that button, and tell us at what line it stops.

    Also, you should post more code, like where is the DataC1 declared, and where you set it to a value (object) ?

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Object Variable or Block Variable not set Error

    A Data Control is automatically populated after Form_Load. You need to use DataC.Refresh to populate the recordset in Form_Load.

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Re: Object Variable or Block Variable not set Error

    Ok... now it works but I get an Overflow Error below...

    Quote Originally Posted by VB.Newb
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim x As Integer
    4. DataC1.Refresh
    5. Dim Cust(7) As Customer
    6.     Do While Not DataC1.Recordset.EOF
    7.         Cust(x).FName = DataC1.Recordset!FName
    8.         Cust(x).LName = DataC1.Recordset!LName
    9.         Cust(x).Address = DataC1.Recordset!Address
    10.         Cust(x).City = DataC1.Recordset!City
    11.         Cust(x).State = DataC1.Recordset!State
    12.         Cust(x).Zip = DataC1.Recordset!Zip '<---Code stops here with Overflow Error
    13.         Cust(x).CNumber = DataC1.Recordset!CNumber
    14.         DataC1.Recordset.MoveNext
    15.         x = x + 1
    16.     Loop
    17.  
    18. Put #1, , Cust
    19.  
    20. End Sub
    Last edited by VB.Newb; Feb 9th, 2006 at 05:50 PM.

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    Did you put a Data control on the form ?

    If you did, name the control DataC1

    See if it works now ?

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Re: Object Variable or Block Variable not set Error

    Yeah, I have a MS ADODC control on the form named "DataC1"

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    Ignore previous post, I see you edited...

    Change the red code to what I have here:
    VB Code:
    1. Private Type Customer
    2.     CNumber As String
    3.     FName As String
    4.     LName As String
    5.     Address As String
    6.     City As String
    7.     State As String
    8.     [COLOR=Red][b]Zip As Long[/b][/COLOR]
    9. End Type

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Re: Object Variable or Block Variable not set Error

    Sweet.. ok.. i'm getting a bit farther.. but i'm still getting an error at the end.. so how do i get it to write the data to a text file?

    here's my code..
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type Customer
    4.     Name As String
    5.     Address As String
    6.     City As String
    7.     state As String
    8.     zipCode As Long
    9. End Type
    10.  
    11.  
    12. Private Sub Command1_Click()
    13.     Dim x As Integer
    14.         Dim Cust(4) As Customer
    15.     dc1.Refresh
    16. Do While Not dc1.Recordset.EOF
    17.     Cust(x).Name = dc1.Recordset!Name
    18.     Cust(x).Address = dc1.Recordset!Address
    19.     Cust(x).City = dc1.Recordset!City
    20.     Cust(x).state = dc1.Recordset!state
    21.     Cust(x).zipCode = dc1.Recordset!zipCode
    22.     dc1.Recordset.MoveNext
    23.     x = x + 1
    24.  
    25. Loop
    26. 'Put 1, , Cust.txt
    27.  
    28. End Sub

  13. #13
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    What format do you want the text file to be ?

    You can do something like:
    VB Code:
    1. Dim K as Long
    2.  
    3. Open "C:\my_text_file.txt" For Binary As Access Write Lock Write #1
    4.  
    5. For K = 0 To Ubound(Cust)
    6.     Put #1, , "Name: " & Cust(K).Name
    7.     Put #1, , "Address: " & Cust(K).Address
    8.     Put #1, , "City: " & Cust(K).City
    9.     Put #1, , "State: " & Cust(K).state
    10.     Put #1, , "ZipCode: " & Cust(K).zipCode
    11. Next K
    12.  
    13. Close #1

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Re: Object Variable or Block Variable not set Error

    Actually, a CSV text file would be awesome.. I tried that code and it worked perfectly but when I hit word wrap in Notepad it got a little garbled.. I'd like the data to be closer together in the text file if i could.. is all so you dont have to scroll so much to read it. thanks again for all the help guys you're helping me pass a class i desperately need to pass

  15. #15
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Object Variable or Block Variable not set Error

    Try this, but i'm not sure it will work, I could not test it since I don't have all your code, and the database to get the values from.

    A CSV is comma delimited, and I think that if a value has a comma in it, then the value will be written in the file encapsulated in quotes.
    So this is what this code does, but I don't know if there are other rules for CSV files.
    VB Code:
    1. Dim K As Long, Str As String
    2.    
    3.     Open "C:\my_text_file.csv" For Binary Access Write Lock Write As #1
    4.        
    5.         ' write header
    6.         Str = "Name,Address,City,State,ZipCode" & vbNewLine
    7.         Put #1, , Str
    8.        
    9.         For K = 0 To UBound(Cust)
    10.             Str = IIf(InStr(1, Cust(K).Name, ",") > 0, """" & Cust(K).Name & """", Cust(K).Name)
    11.             Str = Str & "," & IIf(InStr(1, Cust(K).Address, ",") > 0, """" & Cust(K).Address & """", Cust(K).Address)
    12.             Str = Str & "," & IIf(InStr(1, Cust(K).City, ",") > 0, """" & Cust(K).City & """", Cust(K).City)
    13.             Str = Str & "," & IIf(InStr(1, Cust(K).state, ",") > 0, """" & Cust(K).state & """", Cust(K).state)
    14.             Str = Str & "," & IIf(InStr(1, Cust(K).zipCode, ",") > 0, """" & Cust(K).zipCode & """", Cust(K).zipCode)
    15.            
    16.             Str = Str & vbNewLine
    17.             Put #1, , Str
    18.         Next K
    19.     Close #1

  16. #16
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Object Variable or Block Variable not set Error

    To create a CSV file, open the file for output (or append) and use the Write statement. Write automatically delimits the fields and wraps their values within quotes.

    VB Code:
    1. Open "C:\my_text_file.csv" For Output Access Write Lock Write As #1
    2. ' write header
    3. Write #1, "Name", "Address", "City", "State", "ZipCode"
    4.  
    5. For K = 0 To UBound(Cust)
    6.    Write #1, Cust(K).Name, Cust(K).Address, Cust(K).City, Cust(K).State, Cust(K).ZipCode
    7. Next
    8. Close #1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width