PDA

View Full Version : Visual Basic 6.0 Question


Cap Ologist
09-08-2004, 10:28 PM
Quick question..

I want to make sure that information is entered into a text box named txtFirstName before the user proceeds. I thought that this code would work:

If IsNull(txtFirstName) Then
intMsg = MsgBox("Please enter your first name.", vbOKOnly)
Exit Sub
End If

However, it's not catching it when I try. Am I missing something obvious here?


**Edited Title

lighthousekeeper
09-08-2004, 10:43 PM
It's been a few years, and I rarely store this stuff in my memory, but:

1. Did you try "If txtFirstName.text & "" = "" Then..." ?

2. Do you need the
"intMsg =" at the beginning of the msgbox line?


Hope this helps...

cwilloughby
09-08-2004, 10:48 PM
I think you want...


If txtFirstName = "" Then
MsgBox "Please enter your first name."
Exit Sub
End If


1. In VB the blank string is just "".
2. You don't need to store a return value from the msgbox unless you are looking for something (a particular button press).
3. vbOkOnly is the default MsgBox type.

Let me know if you need any other help.

Cap Ologist
09-08-2004, 11:25 PM
I think you want...


If txtFirstName = "" Then
MsgBox "Please enter your first name."
Exit Sub
End If


1. In VB the blank string is just "".
2. You don't need to store a return value from the msgbox unless you are looking for something (a particular button press).
3. vbOkOnly is the default MsgBox type.

Let me know if you need any other help.

Thanks, I knew that but forgot that I knew that, if that makes any sense. What a wonderful place FOFC is!

cwilloughby
09-08-2004, 11:53 PM
No problem! Glad I could help out.

Cap Ologist
09-16-2004, 04:57 PM
Ok, since there seem to be lots of people who know Visual Basic pretty well here, I have another question. Is there a limit too how many values you can initialize an array with.

For example:

Names = Array("Bob", "Joe", etc., etc.)

I kept getting a message saying the statement was too complex. I've looked in several reference books, but I can't seem to find the answer. Thanks for the help!

Mr. Wednesday
09-16-2004, 05:32 PM
Quick question..

I want to make sure that information is entered into a text box named txtFirstName before the user proceeds. I thought that this code would work:

If IsNull(txtFirstName) Then
intMsg = MsgBox("Please enter your first name.", vbOKOnly)
Exit Sub
End If

However, it's not catching it when I try. Am I missing something obvious here?I don't know that it's obvious, necessarily, particularly if you don't know...

IsNull checks for a Null value, Null being one of the distinct states possible for a Variant (separate from 0, an empty string, Empty, or Missing).

Try using,
If Trim$(txtFirstName) = "" Then
instead (assuming that only spaces isn't something you'd consider valid either). If you want to get really cute, you could also use something like IsAlpha to try to catch non-alphanumeric characters, but I'm not sure how deep you want to go with this.

Cap Ologist
09-16-2004, 05:35 PM
Thanks for the reply, I bumped that thread because I had a different question. I guess I should have just started a new one with the new topic. Thanks for your help, though, I appreciate it.

Mr. Wednesday
09-16-2004, 05:36 PM
Ok, since there seem to be lots of people who know Visual Basic pretty well here, I have another question. Is there a limit too how many values you can initialize an array with.

For example:

Names = Array("Bob", "Joe", etc., etc.)

I kept getting a message saying the statement was too complex. I've looked in several reference books, but I can't seem to find the answer. Thanks for the help!There must be something else going on -- the simple example you've posted does not show that behavior in a simple test case I just tried running.

Sub Main()
'Dim Names() As String
Dim Names As Variant

Names = Array("Joe", "Bob", "Qwerty")
End Sub

With the commented declaration, it gave a type mismatch, as posted it just worked.

Cap Ologist
09-16-2004, 05:40 PM
Right, my thinking is that there is a limit to how many values you can use to initialize an array with. I was just wondering if anyone knew this to be true. I was working on an array containing the names of cities in different states, and I kept getting that error message yesterday.

Mr. Wednesday
09-16-2004, 05:45 PM
If all you were using was a list of names as constants (like in the example I used), I guess you could try breaking up the statement and see what kind of limits you run into.

Ooh, I just had an idea: Depending on how you're doing it, the problem might be the number of line continuations. I think the VB compiler has a hard cap on the number of continuations allowed, and for readability, I wouldn't guess you're trying to do this all on one long line.

Mr. Wednesday
09-17-2004, 03:33 PM
Any luck?

Cap Ologist
09-17-2004, 03:44 PM
Not really, I think there must be some sort of limit to the number of values you can use to initialize an array with. I was trying to set up an array of the cities of Illinois and somewhere between 1100 and 1200 cities I get the "Statement is too complex" message. I know that there is a different message that comes up when you run out of line continuations, so I don't think that is the problem. I've decided to just weed out some of the smaller cities. I really don't think I need that many.

Thanks for your help, and thanks for checking back.

hukarez
09-17-2004, 05:44 PM
Do you happen to have an exact number that you're looking at? For me, I've found it useful for me to declare the maximum value limits in the declarations section of the form/module you're working with.

A little side example that's always worked for me:

Dim Test(10000, 28) As Variant

Of course, this is more along the lines of what I use my array for...which is populating a maximum limit of 10000 records, each record containing 28 fields and the like. I haven't reached the maximum number yet, but I don't get any issues or error messages with my programs.

So if you're doing something along the lines of Cities and States, then a multi-dimensional array like the one above would suffice.

I think something like:

Dim Names(50, 1500) As Variant
Option Base 1

Would probably work best? 50 states, and then whatever number of cities per state or the like.

So say, for California, and 4 cities...you could probably have your program assign (1, 1) and (1, 2), (1, 3) and so forth with multiple cities? Just my thoughts, not sure if this would help any much in the logical compiling of your program.

EDIT: I think I used to run into the same error message before, but managed a workaround by just specifying some number. Probably a downside in the future, but I have a tendency to keep working until I get a stable solution. Then I clean up from then on, and make adjustments, etc..

Wolfpack
09-17-2004, 09:28 PM
Have you considered loading from an external text file and just have a function that runs through it and places the names into array slots? With my Quick Play Football bulk game player program, I just reach out to a CSV file and read line by line all the team data and store it in a multi-dimensional array. For simple things like text files, processing time is very quick.

Wolfpack
09-17-2004, 09:30 PM
Dola...

Another benefit is that it's easier to maintain. Just edit the text files rather than hunt through the source code to fix any problem or make any adjustments.

CSV is probably best because you can then pull the data into Excel and do manipulations on various things if you like.

Cap Ologist
09-17-2004, 10:09 PM
Have you considered loading from an external text file and just have a function that runs through it and places the names into array slots? With my Quick Play Football bulk game player program, I just reach out to a CSV file and read line by line all the team data and store it in a multi-dimensional array. For simple things like text files, processing time is very quick.

Yeah, that's kind of the direction I'm leaning right now. I should have thought of that first because I was typing stuff in straight from an excel spreadsheet. Oh well, I'm glad I realized that after only working through a few states. Thanks for the advice.

Godzilla Blitz
09-17-2004, 11:13 PM
Have you considered loading from an external text file and just have a function that runs through it and places the names into array slots? With my Quick Play Football bulk game player program, I just reach out to a CSV file and read line by line all the team data and store it in a multi-dimensional array. For simple things like text files, processing time is very quick.

Absolutely. What he said.

Mr. Wednesday
09-18-2004, 12:01 PM
Do you happen to have an exact number that you're looking at? For me, I've found it useful for me to declare the maximum value limits in the declarations section of the form/module you're working with.

A little side example that's always worked for me:

Dim Test(10000, 28) As Variant

Of course, this is more along the lines of what I use my array for...which is populating a maximum limit of 10000 records, each record containing 28 fields and the like. I haven't reached the maximum number yet, but I don't get any issues or error messages with my programs.That's a bit inefficient if you don't know beforehand how much stuff you'll be putting in the array, and it also has the drawback of breaking if you should for some reason want to put in more than 10,000 items. Plus, it takes up a bunch of memory, offhand allocating 280,000 variants would use up about 4.48e6 bytes of memory. (Along those lines, variants are a poor choice if you know beforehand that the same thing is going to be going into every member of the array.)

hukarez
09-18-2004, 12:29 PM
That's a bit inefficient if you don't know beforehand how much stuff you'll be putting in the array, and it also has the drawback of breaking if you should for some reason want to put in more than 10,000 items. Plus, it takes up a bunch of memory, offhand allocating 280,000 variants would use up about 4.48e6 bytes of memory. (Along those lines, variants are a poor choice if you know beforehand that the same thing is going to be going into every member of the array.)
Which is actually the case for me a majority of the time. Thankfully, my recordcounts have been around the 10000 record range for the most part. The fields I've often used are usually dependant on the data being entered into the primary test DB I've got going on in the office.

Naturally, I'd be more inclined to utilizing a text file to store my local data...unfortunately, it wouldn't be rational for me to do so if I was going to modify the data later on. The memory issue is something I can live with - there's an error check I've got going on for anything more than 10,000 records.

Some of my fields are datetime fields - I would've been more inclined to declare my array as a string, but figured a variant would be a much safer bet for the time being.

Cap Ologist
09-23-2004, 08:39 PM
Ok, I've been trying to use a random access file, but I'm having a few problems. I must be typing something wrong, because I keep getting really weird values, but I can't figure out what I'm mistyping or omitting.

I want to open a file named Numbers.dat for random.

Open "C:\Numbers.csv" For Random as #1 Len = X

I'm not exactly sure what X is supposed to be.

Is it the number of individual data in the file? The number of lines in the file? The number of data per line?

Then I'm trying to get information.

Get #2, Y, Z

What value should Y be? Is it the line, the data's position as a whole, or the data's position on a line?

Is Z another designator for which data to read? Or is it where the value is stored that is read?

Thanks for helping me understand this a little better. You guys have been great at helping me out so far.

MJ4H
09-23-2004, 08:49 PM
len is the length of each record. for instance if each record is a 15 character string, you have len = 15. Usually I use len = len(whatever datatype im storing).

Cant remember the exact syntax for the get command off the top of my head and im in linux now so i cant look it up.

Cap Ologist
09-23-2004, 08:56 PM
Should you only use len if you are accessing strings?

MJ4H
09-23-2004, 09:01 PM
no i think len is required if you are using random access. all your data should be the same length. like if you are storing a bunch of values for say FirstName(100), you could use len=len(FirstName()). (that syntax may be slightly off -- hard to remember).

should work for whatever data type, even custom data types. wish i could be more specific but its been a few years.

Mr. Wednesday
09-23-2004, 09:21 PM
Ok, I've been trying to use a random access file, but I'm having a few problems. I must be typing something wrong, because I keep getting really weird values, but I can't figure out what I'm mistyping or omitting.

I want to open a file named Numbers.dat for random.

Open "C:\Numbers.csv" For Random as #1 Len = X

I'm not exactly sure what X is supposed to be.I wouldn't recommend using record-based access for a CSV file, which is designed to have variable-length records. I would recommend either using text-based access (input / output, I don't remember if there's something for both) or binary access. If you're using VB6 and you're not trying to do some kind of performance maximization, I'd think it would make the most sense to Open ... For Input, use Line Input to pull a whole line from the file, and then Split (or whatever the library function is called) to break up the line along the comma delimiters.

(I don't mean to say that this will perform poorly, relatively to something hand-tuned, I don't know either way.)

Wolfpack
09-26-2004, 08:41 PM
Try using Microsoft Scripting Runtime library (if memory serves). There are functions and objects in there that make it easy to open a text file and read each line in.

Mr. Wednesday
09-26-2004, 10:48 PM
I'd only resort to the scripting runtime if you need something that's difficult or impossible to do with the built-in file handling functions. Opening a text file and reading it line-by-line are relatively easy to do with the built-in file handling.