[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:
Option Explicit
Private Type Customer
CNumber As String
FName As String
LName As String
Address As String
City As String
State As String
Zip As Integer
End Type
Private Sub Form_Load()
Dim x As Integer
Dim Cust(4) As Customer
Do While Not DataC1.Recordset.EOF
Cust(x).FName = DataC1.Recordset!FName
Cust(x).LName = DataC1.Recordset!LName
Cust(x).Address = DataC1.Recordset!Address
Cust(x).City = DataC1.Recordset!City
Cust(x).State = DataC1.Recordset!State
Cust(x).Zip = DataC1.Recordset!Zip
Cust(x).CNumber = DataC1.Recordset!CNumber
DataC1.Recordset.MoveNext
x = x + 1
Loop
Put #1, , Cust
End Sub
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...
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:
Option Explicit
Private Type Customer
CNumber As String
FName As String
LName As String
Address As String
City As String
State As String
Zip As Integer
End Type
Private Sub Form_Load()
Dim x As Integer
Dim Cust(4) As Customer
Do While Not DataC1.Recordset.EOF
Cust(x).FName = DataC1.Recordset!FName
Cust(x).LName = DataC1.Recordset!LName
Cust(x).Address = DataC1.Recordset!Address
Cust(x).City = DataC1.Recordset!City
Cust(x).State = DataC1.Recordset!State
Cust(x).Zip = DataC1.Recordset!Zip
Cust(x).CNumber = DataC1.Recordset!CNumber
DataC1.Recordset.MoveNext
x = x + 1
Loop
Put #1, , Cust
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
Re: Object Variable or Block Variable not set Error
UPS... stupid me, I just noticed, that Customer is a type NOT class.... :blush:
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:
You should make a dinamic array, and redim by the number of records, like this:
VB Code:
Dim Cust() As Customer
ReDim Cust(DataC1.Recordset.RecordCount)
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:
Dim Cust() As Customer
ReDim Cust(DataC1.Recordset.RecordCount)
but i still got the same error
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) ?
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.
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:
Private Sub Form_Load()
Dim x As Integer
DataC1.Refresh
Dim Cust(7) As Customer
Do While Not DataC1.Recordset.EOF
Cust(x).FName = DataC1.Recordset!FName
Cust(x).LName = DataC1.Recordset!LName
Cust(x).Address = DataC1.Recordset!Address
Cust(x).City = DataC1.Recordset!City
Cust(x).State = DataC1.Recordset!State
Cust(x).Zip = DataC1.Recordset!Zip '<---Code stops here with Overflow Error
Cust(x).CNumber = DataC1.Recordset!CNumber
DataC1.Recordset.MoveNext
x = x + 1
Loop
Put #1, , Cust
End Sub
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 ?
Re: Object Variable or Block Variable not set Error
Yeah, I have a MS ADODC control on the form named "DataC1"
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:
Private Type Customer
CNumber As String
FName As String
LName As String
Address As String
City As String
State As String
[COLOR=Red][b]Zip As Long[/b][/COLOR]
End Type
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:
Option Explicit
Private Type Customer
Name As String
Address As String
City As String
state As String
zipCode As Long
End Type
Private Sub Command1_Click()
Dim x As Integer
Dim Cust(4) As Customer
dc1.Refresh
Do While Not dc1.Recordset.EOF
Cust(x).Name = dc1.Recordset!Name
Cust(x).Address = dc1.Recordset!Address
Cust(x).City = dc1.Recordset!City
Cust(x).state = dc1.Recordset!state
Cust(x).zipCode = dc1.Recordset!zipCode
dc1.Recordset.MoveNext
x = x + 1
Loop
'Put 1, , Cust.txt
End Sub
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:
Dim K as Long
Open "C:\my_text_file.txt" For Binary As Access Write Lock Write #1
For K = 0 To Ubound(Cust)
Put #1, , "Name: " & Cust(K).Name
Put #1, , "Address: " & Cust(K).Address
Put #1, , "City: " & Cust(K).City
Put #1, , "State: " & Cust(K).state
Put #1, , "ZipCode: " & Cust(K).zipCode
Next K
Close #1
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
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:
Dim K As Long, Str As String
Open "C:\my_text_file.csv" For Binary Access Write Lock Write As #1
' write header
Str = "Name,Address,City,State,ZipCode" & vbNewLine
Put #1, , Str
For K = 0 To UBound(Cust)
Str = IIf(InStr(1, Cust(K).Name, ",") > 0, """" & Cust(K).Name & """", Cust(K).Name)
Str = Str & "," & IIf(InStr(1, Cust(K).Address, ",") > 0, """" & Cust(K).Address & """", Cust(K).Address)
Str = Str & "," & IIf(InStr(1, Cust(K).City, ",") > 0, """" & Cust(K).City & """", Cust(K).City)
Str = Str & "," & IIf(InStr(1, Cust(K).state, ",") > 0, """" & Cust(K).state & """", Cust(K).state)
Str = Str & "," & IIf(InStr(1, Cust(K).zipCode, ",") > 0, """" & Cust(K).zipCode & """", Cust(K).zipCode)
Str = Str & vbNewLine
Put #1, , Str
Next K
Close #1
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:
Open "C:\my_text_file.csv" For Output Access Write Lock Write As #1
' write header
Write #1, "Name", "Address", "City", "State", "ZipCode"
For K = 0 To UBound(Cust)
Write #1, Cust(K).Name, Cust(K).Address, Cust(K).City, Cust(K).State, Cust(K).ZipCode
Next
Close #1