Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 07-01-2005, 07:19 PM   #1
Member (8 bit)
 
Join Date: Aug 2003
Location: SoCal
Posts: 178
VBA: ending a function within an if statement

I have a vexing problem that I'm encountering here, in an MS Access VBA function, made vexing by the fact that it seems like it should be very simple, but I just can't solve it.

Basically, I just have an if..then..else statement which is designed as a way of verifying that the user has selected the correct file to import a record from and is importing it into the correct place by comparing a number contained within the file to user-inputted testNumber; the two should match. This is not an essential part of the program, but I think it is important because I anticipate most of these records are going to be imported from portable media, each piece belonging to different researchers, so if somebody imported their file into the wrong record it would be hard to fix without going to the database backup, which I don't have great confidence that they will keep regularly when I'm gone.

Currently the way it functions is it simply compares the two numbers, if they match it tells the user so, if they don't it tells the user so and ends the function. Here's the code:

If testNumber = s Then
MsgBox ("The test number you inputted matched the test number of the file you selected, press OK to continue")
Else
MsgBox ("The test number you inputted did not match the test number of the file you selected, please ensure that these values are correct and try again")
End Function
End If

However, the compiler gives an error on the End Function statement, "Block If without End If." I'm not terribly great with VB, but what I think is happening is that when it encounters the End Function, that terminates the If block as well as the function, which is a syntax error because the If block has been terminated without an End If. What can I do to fix this error, or otherwise end the function if a condition is not met?

My current temporary solution is to have the msgbox advise the user to terminate the function by pressing control-pause/break.

Last edited by jong2k4; 07-01-2005 at 07:27 PM.
jong2k4 is offline   Reply With Quote
Old 07-01-2005, 11:49 PM   #2
Member (9 bit)
 
Join Date: Nov 2004
Location: Flint, MI
Posts: 256
According to page 388 of one of my VB books, try "Exit Function", instead of "End Function".

^fo
foolishone is offline   Reply With Quote
Old 07-05-2005, 12:25 AM   #3
Member (2 bit)
 
Join Date: Jul 2005
Posts: 3
Quote:
Originally Posted by jong2k4
I have a vexing problem that I'm encountering here, in an MS Access VBA function, made vexing by the fact that it seems like it should be very simple, but I just can't solve it.

Basically, I just have an if..then..else statement which is designed as a way of verifying that the user has selected the correct file to import a record from and is importing it into the correct place by comparing a number contained within the file to user-inputted testNumber; the two should match. This is not an essential part of the program, but I think it is important because I anticipate most of these records are going to be imported from portable media, each piece belonging to different researchers, so if somebody imported their file into the wrong record it would be hard to fix without going to the database backup, which I don't have great confidence that they will keep regularly when I'm gone.

Currently the way it functions is it simply compares the two numbers, if they match it tells the user so, if they don't it tells the user so and ends the function. Here's the code:

If testNumber = s Then
MsgBox ("The test number you inputted matched the test number of the file you selected, press OK to continue")
Else
MsgBox ("The test number you inputted did not match the test number of the file you selected, please ensure that these values are correct and try again")
End Function
End If

However, the compiler gives an error on the End Function statement, "Block If without End If." I'm not terribly great with VB, but what I think is happening is that when it encounters the End Function, that terminates the If block as well as the function, which is a syntax error because the If block has been terminated without an End If. What can I do to fix this error, or otherwise end the function if a condition is not met?

My current temporary solution is to have the msgbox advise the user to terminate the function by pressing control-pause/break.
You need to move the "end if" inside the function by putting the "End If" on a blank line above "End Function"

edit: I see now you did not paste the entire function, I believe that Exit Function will work for what you are trying to do.

Last edited by Sapporo; 07-05-2005 at 12:27 AM.
Sapporo is offline   Reply With Quote
Old 07-05-2005, 02:55 PM   #4
Member (8 bit)
 
Join Date: Aug 2003
Location: SoCal
Posts: 178
Exit Function worked quite nicely, thank you both. I thought I had tried it earlier, but there must have been some syntax error in the way when I did, and it works beautifully now.
jong2k4 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:52 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2