Creating functions in VBA

and1_hotsauce

Registered User.
Local time
Yesterday, 19:38
Joined
Aug 29, 2011
Messages
17
Hi,

I am having trouble understanding how the below function (processmessage) works. The program (activexperts) automatically stores a received sms into a database called 'Message' database. The code below retrieves this message FROM message database

But how can a variable be used without first declaring the variable? That is, numMessageID has not been declared. There has been no call made to this function. It stands alone.

Code:
[FONT=Courier New][COLOR=#0000ff]Function[/COLOR][COLOR=#000000] ProcessMessage[/COLOR][COLOR=#000000]( numMessageID[/COLOR][COLOR=#000000] )[/COLOR]

[COLOR=#000000]   [/COLOR][COLOR=#0000ff]Dim[/COLOR][COLOR=#000000] objMessageIn[/COLOR][COLOR=#000000], objMessageOut[/COLOR]

[COLOR=#000000]   [/COLOR][COLOR=#008000]' Open the Message Database[/COLOR]
[COLOR=#000000]   g_objMessageDB.Open[/COLOR]
[COLOR=#000000]   [/COLOR]
[COLOR=#000000]   [/COLOR][COLOR=#008000]' Retrieve the message that has just been received. [/COLOR]
[COLOR=#000000]   [/COLOR][COLOR=#0000ff]Set[/COLOR][COLOR=#000000] objMessageIn[/COLOR][COLOR=#000000]   = g_objMessageDB.FindFirstMessage[/COLOR][COLOR=#000000] ( [/COLOR][COLOR=#808080]"ID = "[/COLOR][COLOR=#000000] & numMessageID[/COLOR][COLOR=#000000] ) [/COLOR]

[COLOR=#000000]   [/COLOR][COLOR=#008000]' Change Status to from Pending to Success. If you don't do it, the message will be processed by subsequent triggers (if defined) because message is still pending[/COLOR]
[COLOR=#000000]   objMessageIn.Status[/COLOR][COLOR=#000000] = g_objConstants.MESSAGESTATUS_SUCCESS[/COLOR]
[COLOR=#000000]   g_objMessageDB.Save[/COLOR][COLOR=#000000] objMessageIn[/COLOR][COLOR=#000000]   [/COLOR]
[COLOR=#000000]    [/COLOR]
[COLOR=#000000]    [/COLOR][COLOR=#008000]' Call ProcessQuery function with input argument objMessageIn where objMessageIn is the sms that was received.  [/COLOR]
[COLOR=#000000]   ProcessQuery[/COLOR][COLOR=#000000] ( objMessageIn[/COLOR][COLOR=#000000] )[/COLOR]
[COLOR=#000000]   [/COLOR]
[COLOR=#000000]   [/COLOR][COLOR=#008000]' Close the Message Database[/COLOR]
[COLOR=#000000]   g_objMessageDB.Close[/COLOR]
[COLOR=#000000]     [/COLOR]
[COLOR=#0000ff]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]Function[/COLOR]
[/FONT]
 
NumMessageID is declared as an argument of the function. It really should have a datatype declared but the programmer has been sloppy. They have also failed to declare a type for the internal variables and not included a return code for the function.
 
thanks for your reply

Can I say this
( "ID = " & numMessageID  ) --> concatenates string ID with numMessageID --> i.e. ID = numMessageID
 
Can I say this
( "ID = " & numMessageID  ) --> concatenates string ID with numMessageID --> i.e. ID = numMessageID

Do you mean in this line?

Code:
[COLOR=#000000]   [/COLOR][COLOR=#0000ff]Set[/COLOR][COLOR=#000000] objMessageIn[/COLOR][COLOR=#000000]   = g_objMessageDB.FindFirstMessage[/COLOR][COLOR=#000000] ( [/COLOR][COLOR=#808080]"ID = "[/COLOR][COLOR=#000000] & numMessageID[/COLOR][COLOR=#000000] )[/COLOR]

Probably, but that really depends on the g_objMessageDB object. It is an instance of a Class with a Method called FindFirstMessage which apparently has a string argument very much like that used in the FindFirst Method of a DAO Recordset.

However without taking a close look at the specifications or code in the Class it is not really possible to know what it is expecting.
 
This function is actually a s sub, as it return no value.

There are several reasons to write a sub as a function. one will be to call it from a macro (you can only run a function from a macro, not a sub). another will be a sloppy programmer :D

as Galaxiom said: numMessageID is a variable sent to the function, and should not be declared in it.
 

Users who are viewing this thread

Back
Top Bottom