Go Back   Access World Forums > Apps and Windows > Visual Basic

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 10-26-2017, 06:32 AM   #1
ryanstens
Newly Registered User
 
Join Date: Oct 2017
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ryanstens is on a distinguished road
Macro isn't executing properly

I need to create a macro that completes the following tasks.

1) Ask for the current car speed using an input box and store this value in a variable.

2) Using one If-Then-ElseIf structure output a message based on the speed.

This is what I have ...

Option Explicit

Sub question1()

Dim Speed As String
Speed = InputBox("What is your current car speed?")
Dim spd As Integer

If (spd < 0) Then
msgbox "this is an invalid speed"

ElseIf (spd > 0 And spd <= 54) Then
msgbox "You are driving too slowly"

ElseIf (spd >= 55 And spd <= 65) Then
msgbox "Your speed is fine"

ElseIf (spd >= 66 And spd <= 70) Then
msgbox "You are driving a little fast, but are probably okay"

Else
msgbox "You are driving too fast"

End If

End Sub


I keep getting "You are driving too fast" as my msgbox no matter what I input. What is the fix here? Thanks.

ryanstens is offline   Reply With Quote
Old 10-26-2017, 06:34 AM   #2
pbaldy
Wino Moderator
 
pbaldy's Avatar
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 32,202
Thanks: 10
Thanked 3,895 Times in 3,838 Posts
pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold
Re: Macro isn't executing properly

You set Speed but test spd.
__________________
Paul
Microsoft Access MVP

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
pbaldy is online now   Reply With Quote
Old 10-26-2017, 06:45 AM   #3
Ranman256
Newly Registered User
 
Join Date: Apr 2015
Location: KY,USA
Posts: 3,159
Thanks: 0
Thanked 690 Times in 675 Posts
Ranman256 will become famous soon enough Ranman256 will become famous soon enough
Re: Macro isn't executing properly

If you use OPTION EXPLICIT in the module header,
then it will catch these variable name misspellings.
Otherwise its a mystery why the code wont work.
The eye wont always catch those spelling differences.

Ranman256 is offline   Reply With Quote
Old 10-26-2017, 07:07 AM   #4
pbaldy
Wino Moderator
 
pbaldy's Avatar
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 32,202
Thanks: 10
Thanked 3,895 Times in 3,838 Posts
pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold pbaldy is a splendid one to behold
Re: Macro isn't executing properly

More on that:

http://www.baldyweb.com/OptionExplicit.htm
__________________
Paul
Microsoft Access MVP

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
pbaldy is online now   Reply With Quote
Old 10-26-2017, 07:32 AM   #5
Minty
AWF VIP
 
Minty's Avatar
 
Join Date: Jul 2013
Location: UK - Wiltshire
Posts: 5,799
Thanks: 140
Thanked 1,565 Times in 1,537 Posts
Minty has a spectacular aura about Minty has a spectacular aura about Minty has a spectacular aura about
Re: Macro isn't executing properly

In fairness - the OP has used option Explicit and has declared both spd and Speed, just then used the wrong one.

As Speed is declared as a string I'm not sure the comparisons will work so he probably needs to add the line

spd = Val(Speed)

before it all kicks off?

__________________
If we have helped please add to our reputation - click the scales symbol on the left, tick 'I approve' and leave a comment.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Minty is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Self Executing Macro pokspeter Macros 3 12-05-2009 02:20 PM
Executing a VB script to run a Macro tylersontag Macros 4 10-07-2009 09:30 AM
Executing code from within a macro? Balr14 Macros 5 01-21-2009 12:58 PM
executing macro by form JamieC Forms 0 04-22-2006 01:42 PM
Macro Not Executing Correctly Melody Macros 2 10-27-2000 06:49 AM




All times are GMT -8. The time now is 06:27 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World