English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am using a user-defined data type to define a record (e.g. Customer name, Customer ID). When I put both fields of the record as strings I get no problem writing the record to a direct access file. But when I put the ID as an integer, the file becomes unreadable and data can't be retrieved again.

e.g.

Private Type CustomerType
CustomerID As Integer
CustomerName As String * 10
End Type

Dim Customer As CustomerType

Open App.Path & "\Customers.dat" for Random As #1 Len = Len(Customer)
NumberOfRecords = LOF(1) / Len(Customer)
Put #1, NumberOfRecords + 1, Customer
Close #1


I was advised to convert the text box that contained the customer ID to an integer before assigning a variable to it.....

Customer.CustomerID = CInt(txtCustomerID.Text)

but it didn't help. MsgBox (VarType(Customer.CustomerID)) showed 2, which is an integer. So what is the problem? Why does it write the ID to my direct access file as a square block instead of a visible number?

2006-11-17 21:14:15 · 3 answers · asked by Older&Wiser 5 in Computers & Internet Programming & Design

Thanks Tamayi M and Justme. I'm digesting your answers.

Tamayi M - my file is the same if I delete it and let the program create another one. I understand what you said about CInt and I'll follow those links. Ta. I'm not quite sure about VarType though, because although I understood what you said about 2 and "2" being different things, MsgBox(2) and MsgBox("2") wouldn't give the same result unless there was a variable called 2, that held the value 2. And VarType should show whether you have "2" or 2 in a variable.

Justme - I had tried assuming that my file should look just like it does - but the trouble is, retrieving the data from it gives incorrect figures. I can't even see where some of them come from. What do you mean by opening the file as a binary file - You mean as well as opening it for random access or instead of, I need to say it's a binary file?

2006-11-19 08:03:14 · update #1

3 answers

it writes to your file as a square box because its an integer and not an ASCII number. when you save it as a string, the number in the string is in ASCII, so you can easily see it in the file. when saved as an integer it is in binary, so you cant see it. that doesnt mean that you CANT save it as an integer, it just means that when you read it back out of the file, you have to read it out as an integer. another thing you NEED to do is open, and save the file as a binary file.

2006-11-18 01:57:05 · answer #1 · answered by justme 7 · 0 0

Random Access Files

I suspect the problem with the corrupt files could be that you changed the record type after you had previously written some records to the file. What happens in your existing code if you clear the Customers.dat file and start a clean one using the new CustomerType which uses an integer for the ID?

Have a look at these links:

http://www.vbcity.com/forums/topic.asp?tid=6387
http://www.informit.com/articles/article.asp?p=20992&seqNum=5&rl=1

I think the vbCity article is best for you.


OTHER NOTES:

How you put data into your CustomerType does not affect whether or not you can read the file back or not.

Customer.CustomerID = CInt(txtCustomerID.Text)

will achieve the same as

Customer.CustomerID = txtCustomerID.Text

in VB because of implicit conversions between datatypes. In both cases if txtCustomerID does not contain an integer you will get an error anyway. Your intepretation of what MsgBox (VarType(Customer.CustomerID)) displaing a 2 means is also not entirely correct. MsgBox(2) and MsgBox("2") will display the same but 2 and "2" are not the same, one is a string and one is a n integer. However, in the VB6 world, they are made to work as if they were the same because VB6 implcitly does a lot of type conversions for you. Use a language like C++ and "2" and 2 become dr jekyl and dr hide.

2006-11-17 22:38:43 · answer #2 · answered by Tamayi M 2 · 0 1

the place does DEJ come from? in case you write out the stairs you may desire to get from the call and start date to the code you choose for, you're transforming into this device and you will only ought to code it in VB6. you need to use InStr to locate the area between first and final names, Mid$ to pluck out any character or characters and only build the code from that. (Use a DTPicker to get the start date and you're transforming into a fastened format date, to make it much less confusing to %. out areas of it.)

2016-12-29 04:37:12 · answer #3 · answered by langhorne 3 · 0 0

fedest.com, questions and answers