![]() |
Visual Basic 6.0 Question
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 |
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... |
I think you want...
Code:
If txtFirstName = "" Then1. 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. |
Quote:
Thanks, I knew that but forgot that I knew that, if that makes any sense. What a wonderful place FOFC is! |
No problem! Glad I could help out.
|
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! |
Quote:
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. |
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.
|
Quote:
Code:
Sub Main()With the commented declaration, it gave a type mismatch, as posted it just worked. |
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.
|
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. |
Any luck?
|
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. |
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.. |
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.
|
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. |
Quote:
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. |
Quote:
Absolutely. What he said. |
Quote:
|
Quote:
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. |
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. |
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. |
Should you only use len if you are accessing strings?
|
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. |
Quote:
(I don't mean to say that this will perform poorly, relatively to something hand-tuned, I don't know either way.) |
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.
|
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.
|
| All times are GMT -5. The time now is 02:10 AM. |
Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.