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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining Constants from InputBox

Status
Not open for further replies.

kencat742

Programmer
Jun 30, 2005
23
US
I want to have the value for my constant be based off the number from an InputBox, Currently I have this in my code:
Option Explicit
Dim chan2 As Double, tmes2 As Double
Private Sub Form_Load()
chan2 = Val(InputBox("How many channels?", "Channels", 16)) + 1
tmes2 = Val(InputBox("How many time changes?", "Times", 10)) + 1
Const chan As Integer = chan2
Const tmes As Integer = tmes2
End Sub

When i go to run it, I get an error message saying "Constant expression Required" I have looked through the FAQs, but couldn't find anything about constants. Any suggestions would be greatly appreciated.
Thanks :)
 
If you want the value to change, ten it is not by definition a constant. Use a variable.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I need it to be a constant because I use the value for defining an array and when i tried just using a variable it said "constant value required". The value will not change through the program, just be initiated at the beginning based on the answer to the inputbox.
 
I define arrays using variables all the time. Can you post that part of your code? That is where your real problem is.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Do a search for Redim.


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Option Explicit
Dim chan2 As Double, tmes2 As Double


Private Sub Form_Load()

chan2 = Val(InputBox("How many channels?", "Channels", 16)) + 1
tmes2 = Val(InputBox("How many time changes?", "Times", 10)) + 1
Const chan As Integer = chan2
Const tmes As Integer = tmes2

End Sub
Private Sub cmdEnd_Click()
End
End Sub

Private Sub cmdGet_Click()
Dim i As Integer, j As Integer, k As Integer
Dim listtime(tmes + 1) As Variant
Dim ch(tmes + 1) As Variant

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application

Dim xlWB As Excel.Workbook
Set xlWB = xlApp.Workbooks.Open("H:\testsheet.xls")

Dim xlWS As Excel.Worksheet
Set xlWS = xlWB.Worksheets("Sheet1")



For j = 1 To tmes
For k = 1 To chan
listtime(j) = listtime(j) & (xlWS.Range(Chr(64 + k) & j).Value) & " " & Chr(9)
Next k
Next j

For i = 1 To tmes
txtFile.Text = txtFile.Text & listtime(i) & vbNewLine
Next i

For j = 2 To tmes
ch(j) = Mid$(listtime(j), 1, 4)
txtnum.Text = txtnum.Text & ch(j) & vbNewLine
Next j

' Close Up shop:
Set xlWS = Nothing

xlWB.Close
Set xlWB = Nothing

xlApp.Quit
Set xlApp = Nothing

End Sub
 
Both blocks of code will create an array of the same size. One uses a constant and the other uses a variable. The difference is that you dim the array without bounds and then redim with the bounds you want it to be.


Const MyConst As Integer = 200
Dim MyArray(MyConst) As String

' OR

Dim MaxArray As Integer
Dim VariableArray() As String

MaxArray = 200
ReDim VariableArray(MaxArray)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks, I forgot that I could dim it originally without putting a number in it. I think its working for now...
 
Conceptually, there are two types of arrays: static and dynamic. Static arrays never change size in their lifetime; dynamic arrays do. If your needs are for dynamic arrays, it's best to do them in the generally accepted manner, so other people can maintain your code more easily.

So, you're quite correct to use a dynamic array definition in this case, and use redim to size it. I wouldn't do it the way that you had it originally; although it works, it will be harder for other people to sort through.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top