Log in

View Full Version : Ideal Gas Program



JeffreyRS
12-08-2008, 10:08 PM
Hi everyone, I am relatively new to vba coding, but i have had experience with coding in other languages, so I'm not completely ignorant. I have been coding a program to run an the ideal gas law, solving for both pressure and volume.

Here is the main code (module 1), (also note that i have visual basic 6.5, version 1024):

Public t As Double
Public n As Double
Public p As Double
Public r As Double
Public v As Double
Public units As String



Function PressCalc()
n = PressureCalc.MoleInputP.Text
v = PressureCalc.VolumeInputP.Text
t = PressureCalc.TempInputP.Text
If v = 0 Then
ErrorBoxPressure.Show
Else
Select Case PressureCalc.SIUnitsP
Case PressureCalc.SIUnitsP.Value = True
r = 8.314 ' in terms of m^3*Pa/g-mol*K
p = n * r * t / v
units = Pa
DispAnsP.AnswerP.Caption = MainCode.p
DispAnsP.UnitsP.Caption = MainCode.units
Case Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
p = n * r * t / v
units = psia
DispAnsP.AnswerP.Caption = MainCode.p
DispAnsP.UnitsP.Caption = MainCode.units
End Select
DispAnsP.Show
Unload PressureCalc
End If
End Function

Function VolCalc()
t = volumecalc.TempInput.Text
n = volumecalc.MoleInput.Text
p = volumecalc.PressureInput.Text
If p = 0 Then
ErrorBoxVolume.Show
Else
Select Case volumecalc.SIUnitsV
Case volumecalc.SIUnitsV.Value = True
r = 8.314 ' in terms of m^3*Pa/g-mol*K
v = n * r * t / p
units = m ^ 3
DispAnsV.AnswerV.Caption = MainCode.v
DispAnV.UnitsV.Caption = MainCode.units
Case Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
v = n * r * t / p
units = ft ^ 3
DispAnsV.AnswerV.Caption = MainCode.v
DispAnsV.UnitsV.Caption = MainCode.units
End Select
DispAnsV.Show
End If
End Function
Function clipboard() ' copies the answer to the clipboard
Dim copytoboard As DataObject
Set copytoboard = New DataObject
copytoboard.SetText "MainCode.p"
copytoboard.PutInClipboard
End Function
To give everyone a better feel for how the program is designed, the first userform asks the user to calculate either pressure or volume, where he or she inputs the values (t, n, p or v) and then clicks calculate, which runs a function I've defined (presscalc, volcalc). Then its sends the user to a display answer userform.

I've been primarily been getting a "runtime error 13; type mismatch," which leads me to believe that I have falsely defined some variables, but they all defined correctly, to me at least.

Also, when I do get an answer, if when i click the exit program button on the display answer userform, i get a "runtime error 424; object required." It makes no sense to me because the only coding on that userform tells it to unload the program. Here is the code anyways:


Private Sub CancelP_Click()
Unload DispAnsP
PressureCalc.Show
End Sub

Private Sub ExitP_Click()
Unload DispAnsP
End Sub
I'll also include the code for one of the input userforms, just to be complete:

Private Sub AmericanUnitsP_Click()
TempLabelP.Caption = "Temperature (?R)"
MoleLabelP.Caption = "Moles (lb-mol)"
VolumeLabelP.Caption = "Volume (ft^3)"
End Sub
Private Sub CalculatePC_Click()
Unload PressureCalc
End Sub

Private Sub CancelPC_Click()
Unload PressureCalc
Choice.Show
End Sub

Private Sub CalculateP_Click()
PressCalc.Run
Unload PressureCalc
End Sub

Private Sub ExitPC_Click()
Unload PressureCalc
End Sub

Private Sub SIUnitsP_Click()
TempLabelP.Caption = "Temperature (K)"
MoleLabelP.Caption = "Moles (g-mol)"
VolumeLabelP.Caption = "Volume (m^3)"
End Sub

''This code prevents letters from being inputted into the textbox
Private Sub MoleInputP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.MoleInputP.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select

End Sub



Private Sub UserForm_Click()

End Sub
''This code prevents letters from being inputted into the textbox
Private Sub VolumeInput_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.VolumeInput.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select

End Sub
''This code prevents letters from being inputted into the textbox
Private Sub TempInputP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
If InStr(1, Me.TempInputP.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select

End Sub
Also note that I've included a toggle button to switch between SI and American Engineering Units, which works in the input screen, but won't calculate (yet another bug I don't get)

To top it off, the units string I've defined won't display in the final display box either. I have no idea why.

If anyone could help debug this biatch, words couldn't describe how grateful I would be; I don't want to think about the time I have put into looking over each line, only to think that the program should run flawlessly.

Again thank you everyone for considering my conundrum :hi:

PS Also, I would be happy to email this file if anyone wants to take a further look at it.

Sal Paradise
12-09-2008, 01:06 AM
At what point is the error coming up? Which variable? Care to narrow things down a bit for us?

Tommy
12-09-2008, 06:56 AM
Hi JeffreyRS, :hi:

Welcome to VBAX!

Just looking I see:
Public t As Double
Public n As Double
Public p As Double
Public r As Double
Public v As Double

Then you set it equal to some text:
n = PressureCalc.MoleInputP.Text
v = PressureCalc.VolumeInputP.Text
t = PressureCalc.TempInputP.Text

in both functions PressCalc and VolCalc. This is the type mismatch. Let's see where that gets you and then we'll go from there.

JeffreyRS
12-09-2008, 07:30 AM
hey.

thank you for the response you guys. Runtime error 13 occurs at

t=pressurecalc.tempinput.text

so that makes sense for a type mismatch. What would the correct object be? I guess .value but I've tried that too and I still get another error later for runtime error 424: object required.

Thank you for that info, because multiplying text doesn't make sense obviously.

ps runtime error 424 occurs at

Private Sub CalculateP_Click()
PressCalc.Run <==here
Unload PressureCalc
End Sub

Tommy
12-09-2008, 09:07 AM
n = Cdbl(PressureCalc.MoleInputP.Text)
v = Cdbl(PressureCalc.VolumeInputP.Text)
t = Cdbl(PressureCalc.TempInputP.Text)



PressCalc

not

PressCalc.Run

JeffreyRS
12-09-2008, 12:56 PM
Ugh this program is bugged out.

Thank you for the input Tommy; I've made your changes, which have seemed to make it function a little better.

When I run volcalc i still get a runtime error 13: type mismatch for:

t = CDbl(volumecalc.TempInput.Text)

It works for presscalc though, which makes no sense because it's essentially the same code. Also, when i enter in nothing into the pressure input userform, i get a type mismatch--which really makes no sense.


Pressing cancel on the dispans userform gives me a runtime error '400', which says form already displayed, cannot show modally.

I assume this means that the form is already engaged. I just want to close the dispans userform so that the user can enter in other values. The error leads me to believe that the function presscalc has engaged the userform in some way as to not allow it to reopen? I'm really not sure though.
Also, my string variable 'units' still isn't showing...anyone have an idea why? Ugh this code is eating my lunch.

Tommy
12-09-2008, 02:31 PM
Can you post your project? I may have some time tonight to look at it.
Your units problem may be the assignments are not valid. Well the variables that units are being assigned to are not posted so....

units = Pa
units = psia
units = m ^ 3
units = ft ^ 3


Pressing cancel on the dispans userform gives me a runtime error '400', which says form already displayed, cannot show modally.
Where do you unload it?

Don't forget that we can't see what you see so you have to be clear and state you problems clearly with the offending code (Bad code:whip )

JeffreyRS
12-09-2008, 03:45 PM
I just got it to work!

I changed the calculate button to include this code, which made it work better:

With Me
If Len(.TempInput) = 0 Or _
Len(.PressureInput) = 0 Or _
Len(.MoleInput) = 0 Then

MsgBox "Please enter temperature, pressure and moles.", vbExclamation, _
"Error: Missing Data"
Exit Sub
ElseIf .PressureInput = 0 Then
MsgBox "Pressure cannot be zero.", vbExclamation, "Error: Pressure"
Exit Sub
End If
End With

I also changed the main code to this:

Function VolCalc()
t = CDbl(volumecalc.TempInput.Text)
n = CDbl(volumecalc.MoleInput.Text)
p = CDbl(volumecalc.PressureInput.Text)

' if it's SI Units
If volumecalc.SIUnitsV Then
r = 8.314 ' in terms of m^3*Pa/g-mol*K
v = n * r * t / p
DispAnsV.lbl_Units.Caption = "m ^ 3"
' otherwise it's AE Units
Else
r = 10.73516 ' in terms of psia*ft^3/lb-mol*degrees R
v = n * r * t / p
DispAnsV.lbl_Units.Caption = "ft ^ 3"
End If

DispAnsV.AnswerV.Caption = MainCode.v

DispAnsV.Show
End Function

which made it alot simpler....thanks for all the help!!