Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning variable value to txtbox prob

Status
Not open for further replies.

hubud

Instructor
Oct 18, 2002
123
0
0
GB
I have spent some time trying to assign the calculated value of a variable to a results txt box with no success. The debug shows that the variable does hold the data but the txtbox will not accept it, though it throws up no errors. the following code is the problem...any ideas out there?
Dim result As Integer
result = (95 / Val(txtElement1.Text)) + (95 / Val(txtElement2.Text)) + (95 / Val(txtElement3.Text))



'If OpPpm is ticked then run this code

If OpPpm.Value = True Then

If CmbEfficiency.Text = "stechiometric" Then

If CmbEfficiency.Text = "phd-50/hd-2000" Then

If CmbEfficiency.Text = "fif-50" Then

If CmbEfficiency.Text = "bags" Then

TxtResults.Text = result / 0.2

End If

TxtResults.Text = result / 0.4

End If

TxtResults.Text = result / 0.6

End If

TxtResults.Text = result

End If

End If

this is just one of a list that i am running that all have the same problem. It's the txtresults.text in the 'TxtResults.Text = result' statment that is causing the problem.

cheers

simmo
 
Surely there's a basic flaw in this code?

If CmbEfficiency.Text is "stechiometric" then how can it possibly also be "phd-50/hd-2000" aswell? Or "fif-50" or "bags"? It can only be one of these values, not all 4. It's like saying:

If (a& = 1) Then
If (a& = 2) Then
If (a& = 3) Then
Msgbox "Hello"
End If
End If
End If

You'll never get the message because if a& =1 then it can't also = 2, and so on...

Try something like this:

If (Instr(CmbEfficiency.Text, &quot;stechiometric&quot;) <> 0) Then
If (Instr(CmbEfficiency.Text, &quot;phd-50/hd-2000&quot;) <> 0) Then
...

and so on.

This will work through the conditions if the required text is found within the textbox.

- Andy.
 
or:-

If OpPpm.Value = True Then

If CmbEfficiency.Text = &quot;stechiometric&quot; Then

elseIf CmbEfficiency.Text = &quot;phd-50/hd-2000&quot; Then

elseIf CmbEfficiency.Text = &quot;fif-50&quot; Then

elseIf CmbEfficiency.Text = &quot;bags&quot; Then

else

end if

good luck

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 
Just tried both the above. I follow the logic problem originally however having run both these variations I am still getting the problem that the 'txtresults' text box is not being assigned the values from the 'result' variable. Below is attached the new code as per suggestions. Debug is showing that the variable declaration and assignation of values is not happening even though the values being passed to it appear to hold the correct values.

Dim result As Integer

result = (95 / Val(txtElement1.Text)) + (95 / Val(txtElement2.Text)) + (95 / Val(txtElement3.Text))

TxtResults.Text = result

ElseIf OpPpm.Value = True And _
CmbEfficiency.Text = &quot;phd-50/hd-2000&quot; Then

TxtResults.Text = result / 0.6

ElseIf OpPpm.Value = True And _
CmbEfficiency.Text = &quot;fif-50&quot; Then

TxtResults.Text = result / 0.4


ElseIf OpPpm.Value = True And _
CmbEfficiency.Text = &quot;bags&quot; Then

TxtResults.Text = result / 0.2

any ideas. thanks for the help so far.


Simmo
 
im assuming somewhere in your code you have the if and end if statement?!?

Dim result As Integer

result = (95 / Val(txtElement1.Text)) + (95 / Val(txtElement2.Text)) + (95 / Val(txtElement3.Text))

TxtResults.Text = result
if something then

ElseIf OpPpm.Value = True And _
CmbEfficiency.Text = &quot;phd-50/hd-2000&quot; Then

TxtResults.Text = result / 0.6

ElseIf OpPpm.Value = True And _
CmbEfficiency.Text = &quot;fif-50&quot; Then

TxtResults.Text = result / 0.4


ElseIf OpPpm.Value = True And _
CmbEfficiency.Text = &quot;bags&quot; Then

TxtResults.Text = result / 0.2
End if

you could always stick a breakpoint in each of the branches of your if statement to check the code is actually going there.

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 
Hi Simmo,

The bottom line is this - the textbox will display something, anything, provided that it reaches one of the lines that sets a value in it - even if Result is 0 the textbox will contain &quot;0&quot;. If your textbox is empty then the code can't be reaching a point where it gets set.

Try placing TxtResults.Text = &quot;hello&quot; at the start of the routine, and see if it changes to empty - if it doesn't then it must be because none of your conditions are met.

If cmbEfficiency is a combobox, try instead of cmbEfficiency.Text:

cmbEfficiency.List(cmbEfficiency.ListIndex)

- Andy.
 
Simmo ...
I would do something like this

Private Sub Command1_Click()
Dim sHoldingVariable As String
If OpPpm.Value = True Then
Select Case CmbEfficiency.Text
Case &quot;phd-50/hd-2000&quot;
'sHoldingVariable = Do Something
Case &quot;stechiometric&quot;
'sHoldingVariable = Do Something Else
Case &quot;fif-50&quot;
'sHoldingVariable = Do Something Different
Case Else
'sHoldingVariable = Do Something Else Different
End Select
Text1.Text = sHoldingVariable
Else
Text1.Text = &quot;Nothing Selected&quot;
End If
End Sub
Michael
 
cheers folks, i've tried hobbitk's advice and have assigned the data to a second variable(see below). Surprise surprise, the second variable holds nothing. The data is not being assigned to the correct value passed by the calculation. I really can't see what is wrong with this one. If I had any hair I would be pulling it out. below is the partial code section that is repeated twice.

ElseIf OpPpm.value = True Then
dim value as string
Dim result As String

result = (95 / Val(txtElement1.Text)) + (95 / Val(txtElement2.Text)) + (95 / Val(txtElement3.Text))

Select Case CmbEfficiency.Text
Case &quot;stechiometric&quot;
value = result
Case &quot;phd-50/hd-2000&quot;
value = result / 0.5
Case &quot;fif-50&quot;
value = result / 0.4
Case &quot;bags&quot;
value = result / 0.2
End Select
TxtResults.Text = value
end if

&quot;result&quot; is ok, &quot;value&quot; is not receiving the &quot;result&quot; value.
In debug it appears to take no notice of the fact that cmbeffiency is matching stechiometric and jumps through the whole select case until the end, as if I haven't inputted stechiometric at all.

appreciate the time

simmo
 
can i confirm that cmbefficiency.list(cmbeffiency.listindex)
function is to used like follows:

Select Case CmbEfficiency.List(CmbEfficiency.ListIndex)
Case 0
value = result
Case 1
value = result / 0.5
Case 2
value = result / 0.4
Case 3
value = result / 0.2
End Select
TxtResults.Text = value

simmo
 
You haven't got 'hidden spaces' in your CmbEfficiency textbox? Try using the Trim function if you have.

Also check *exactly* what's in the CmbEfficiency.Text with a debug statement like:
[tt]Debug.Print &quot;*&quot; & CmbEfficiency.Text & &quot;*&quot;[/tt]


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
why are result and value strings... any particular reason??

dim result as double
dim value as double

result = cdbl((95 / Val(txtElement1.Text)) + (95 / Val(txtElement2.Text)) + (95 / Val(txtElement3.Text)))
debug.print result

debug.print Value
txtResults.text=cstr(value)

it could quite easily be that the value is being lost in the conversions...

and as johnwm says make sure there are no spaces in the efficiency box.

i imagine when you sus this youll kick yourself!

good luck!!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 
Thanks for the help so far. I've just had an idea. I am loading the combo box in the formload with the following syntax.
CmbEfficiency.AddItem &quot;Stechiometric&quot;, 0
CmbEfficiency.AddItem &quot;Phd-50/HD-2000&quot;, 1
CmbEfficiency.AddItem &quot;Fif-50&quot;, 2
CmbEfficiency.AddItem &quot;Bags&quot;, 3
this is the first little programme I've done and I'm wandering if it's something to do with this. should I be loading this else where.

simmo
 
i dont see a problem with the form load event and adding to a combo!!

just out of interest what are txtElement1/2/3... are they decimal numbers or nice round numbers??

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 
sorry scratch that last one ive just reread the post!!

select case CmbEfficiency.List(CmbEfficiency.ListIndex)

will return the string value so having case 0 case 1 case 2 etc wont work

try replacing that line with

select case cmbefficiency.listindex

or alternatively replace
case 0 with case &quot;stechiometric&quot;
case 1 with case &quot;Phd-50/HD-2000&quot;
etc etc...

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 
Hmmmm, I may be wrong but this is what I see.

If this is what you have in your list

CmbEfficiency.AddItem &quot;Stechiometric&quot;, 0
CmbEfficiency.AddItem &quot;Phd-50/HD-2000&quot;, 1
CmbEfficiency.AddItem &quot;Fif-50&quot;, 2
CmbEfficiency.AddItem &quot;Bags&quot;, 3

And this is what your looking for

Case &quot;stechiometric&quot;
Case &quot;phd-50/hd-2000&quot;
Case &quot;fif-50&quot;
Case &quot;bags&quot;

Then you'll never get a match.
Try this

Case &quot;Stechiometric&quot;
Case &quot;Phd-50/HD-2000&quot;
Case &quot;Fif-50&quot;
Case &quot;Bags&quot;

The match IS case sensative and thus, must match exactly.

Just a thought.
 

ClickHere,

You are quite right about it being case sensitive but you can get around it by placing...
[tt]
Option Compare Text
[/tt]

up top in your general declartions section... from vb's help
[tt]
Option Compare Statement


Used at module level to declare the default comparison method to use when string data is compared.

Syntax

Option Compare {Binary | Text | Database}

Remarks

If used, the Option Compare statement must appear in a module before any procedures.

The Option Compare statement specifies the string comparison method (Binary, Text, or Database) for a module. If a module doesn't include an Option Compare statement, the default text comparison method is Binary.

Option Compare Binary results in string comparisons based on a sort order derived from the internal binary representations of the characters. In Microsoft Windows, sort order is determined by the code page. A typical binary sort order is shown in the following example:

A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø

Option Compare Text results in string comparisons based on a case-insensitive text sort order determined by your system's locale. When the same characters are sorted using Option Compare Text, the following text sort order is produced:

(A=a) < ( À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)

Option Compare Database can only be used within Microsoft Access. This results in string comparisons based on the sort order determined by the locale ID of the database where the string comparisons occur.

[/tt]

Good Luck To All

 
Gentlemen I would like to thank you all. The problem was one of case sensitivity.

adoozer was right, a minor prob making me lose hair.

cheers folks

simmo
 
Bravo, vb5prgrmr

That solution never came to mind. You know how it is, you look past the simple things for something complex.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top