Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2003, 05:45 AM
Authorized User
 
Join Date: Sep 2003
Location: Ipswich UK, , United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MattLeek
Default 'Constant' Error

I am learning VBA and have got to chapter 4 (not far i know). I started trying to create a quiz for something to do and to get a better understanding of what i know so far but i get an error about constants. The bit i am having trouble with is:

'START number of questions
    Const csNumQ = InputBox("How many questions do you want" & _
        "out of 20?")

    If intNumQ > 20 Then
        MsgBox ("You chose too many. So we will give you 20 questions")
        intNumQ = 20
    ElseIf intNumQ < 0 Then
        MsgBox ("You can't have a negative number of questions!" & _
            "so you obviously don't want to do this!")
        intNumQ = 0
    End If
    'END number of questions
    Dim intLoop As Integer
    Dim istrResponce(1 To intNumQ) As String
    'START MsgBox's
    For intLoop = 1 To intNumQ
        strResponce(intLoop) = InputBox(" " & strQs(intLoop) & " " & _
             "a: " & strAnsA(intLoop) & " ", "Question " & intLoop)
    Next intLoop
    'END MsgBox's

I hadn't put the Const intNumQ in until it gave me and error saying: "Compile Error Constant Expression Required" and hilighted the bold bit:

Dim istrResponce(1 To intNumQ) As String

I added in the Const intNumQ and now it says the same thing but hilights the InputBox part.

I am guessing that there is probably a simple explanation so any help would be appreciated

Thanks
__________________
Thanks,
Matthew
 
Old September 14th, 2003, 07:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oxford, , United Kingdom.
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Matt

The problem is that you cannot use variables in a Dim statement. Whatever you put in a Dim statement has to be fixed at compile time, so the compiler can work out how much memory to put aside for storage and other esoteric compiler stuff.

The good news is, there's a way around this:

Code:
'Put all your Dim statements at the top to make the code more readable
    Dim intLoop As Integer
'Dim an array with no dimensions. This tells the compiler that we will
'use an array, but we don't know what size it's going to be
    Dim istrResponce() As String

'START number of questions
    Const csNumQ = InputBox("How many questions do you want" & _
        "out of 20?")
    
    If intNumQ > 20 Then
        MsgBox ("You chose too many. So we will give you 20 questions")
        intNumQ = 20
    ElseIf intNumQ < 0 Then
        MsgBox ("You can't have a negative number of questions!" & _
            "so you obviously don't want to do this!")
        intNumQ = 0
    End If
    'END number of questions
    Redim istrResponce(1 to intNumQ) 
    'START MsgBox's
    For intLoop = 1 To intNumQ
        strResponce(intLoop) = InputBox(" " & strQs(intLoop) & " " & _
             "a: " & strAnsA(intLoop) & " ", "Question " & intLoop)
    Next intLoop
    'END MsgBox's
One other possible problem is that you seem to have two variables
Code:
istrResponce(
and
Code:
strResponce(
with very similar names - are these supposed to be the same?


Brian Skelton
Braxis Computer Services Ltd.
 
Old September 14th, 2003, 08:56 AM
Authorized User
 
Join Date: Sep 2003
Location: Ipswich UK, , United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MattLeek
Default

Thanks very much, it works now. istrResponce and strResponce were the same. I guess i should try and make my scripts more readable and not make silly mistakes (i spotted quite a few!)

So thanks again

Matthew Leek




Similar Threads
Thread Thread Starter Forum Replies Last Post
constant reference in C#? texasraven C# 19 December 13th, 2007 08:28 AM
Constant pointer and pointer to a constant amit_mande@yahoo.com C++ Programming 2 June 5th, 2007 01:39 AM
Unterminated string constant error mateenmohd Classic ASP Basics 2 May 30th, 2007 02:36 AM
Constant in VBScript savoym VBScript 2 May 18th, 2005 06:53 AM
Backend as a constant Steven Access 1 June 29th, 2003 11:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.