PDA

View Full Version : Solved: If, End If; UserForm textbox to numerical variable



SilverBack
12-03-2005, 10:28 PM
I'll make this one quick.

I have two problems:

First, I have a list of variables that take their numerical assignments from a Userform textbox. These numbers may be both positive or negative. For this to happen, how would I go about getting a number in the form of a string into a variable expecting a number? I'm thinking of Int, Fix, and CStr, but Int and Fix seem a little iffy if I have to discern between positive and negative. Is an If statement required to differentiate between using Int and Fix?

Secondly, with the same list of variables, I need to assign the textbox value to a corresponding variable only if there is something in the box. The idea is to prevent the user from having to enter all values when only one equation needs to be solved. I'm wondering if each variable will require its own If...Endif statement, or if there's a shorter way to do it.

I'm new to calling sub-routines so I've got a few bumps to hit. I've attached the code, and to make it a little easier to understand, I've placed comments throughout. If anyone can tell me what I've done that doesn't need to be done, or what I need to do, I'd greatly appreciate it. It sure seems like I'm passing a lot of variables!

Thanks!

mdmackillop
12-04-2005, 03:47 AM
Hi Silverback,
I've revised your code a little
1. Moving the functions to the userform
2. Declaring the variables first (outwith any sub or function). This way they are available to all your routines and hold their value until needed.
3. Deleted calculated items from your Call statement. You need only pass values for use by the functions etc. eg, J, K are set within the functions
4. Deleted the textbox to variable conversions. You can pass the textbox values directly. The receiving function will deal with the values according to the type set by the declared variables.
5. Note that parameter names do not need to be the same when passing. If they are not the same, VB will use them in the order entered, so needs a little care. Using the same name means any order can be used.
6. SListboxOut changed to String
7. Textbox1.AddItem line fixed.
8. I added data for my test purposes as you will see. You can add 0 as a default value to avoid empty textboxes if this is appropriate. (watching for division by 0 of course)
9. (not shown) Rather than CString your results, use formatting to present the desired appearance.
eg. resAir_Density = Format(sAir_Density, "#,##0.00")

Happy landings!
MD

SilverBack
12-04-2005, 02:13 PM
Thanks for the help again. I was working with this program again this morning and remembered that the listbox on the right side of the form is actually supposed to be a textbox. The textbox is supposed to reflect the time increment on the left and the remaining rocket motor weight as the propellant burns down in relation to the time increment, on the right.


L = 0

TextBox1.Text = "Time" & vbTab & "Remaining Propellant"
For L = 0 To J

TextBox1.Text = strTimeArray(L) & vbTab & strPropellantArray(L) & vbCr
Next L


Above is what I'm working with now. I've checked my books and again, writing multiple lines to a textbox is a first. So far I've only had to do one line. I know I haven't included the values for the arrays, but I have stepped through the program and the array assignments work correctly, this is just where I have to stick those array values in the textbox.

Your revision sure looks a lot cleaner than mine! I'll change my code around when I get done here.

Thanks again :)

mdmackillop
12-04-2005, 02:24 PM
You'll need to set the textbox MultiLine property to true. To write in the result try


TextBox1.Text = "Time" & vbTab & "Remaining Propellant" & vbcr
For L = 0 To J
TextBox1.Text = TextBox1.Text & strTimeArray(L) & vbTab & _
strPropellantArray(L) & vbCr
Next L

SilverBack
12-04-2005, 02:44 PM
Sweet, is there reason you can see that I would get "subscript out of range"?

There is also a new problem with getting decimals into the "burn rate" and "increment" fields of the form.

mdmackillop
12-04-2005, 03:07 PM
Regarding "Subscript out of range" Arrays are normally base 0, so a 4 item array has items 0, 1, 2 & 3. It is common for increments etc. to finish one number higher so you may need to use "From 0 to J -1" . Arrays can be made to start at 1 by using Option Base 1 at the head of your code, in which case From 1 to J may be correct.

mdmackillop
12-04-2005, 03:16 PM
I can't see a reason for the decimal problem as the textboxes are text fields. Can you repost your version of the form?

SilverBack
12-04-2005, 03:36 PM
Can't believe I forgot that. Thanks again :)