VB Help Needed

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
lebrocoli
Pro Scripter
Posts: 53
Joined: Tue Sep 06, 2005 9:28 pm

VB Help Needed

Post by lebrocoli » Thu Oct 25, 2007 8:35 pm

Ok. Begineer question again:

I seem to be able to open my db connection. Now, how do I assign the result of the SQL query to a variable so I can then compare the answer with what I am expecting? I am not sure what I should put around the part in Bold. Thanks

Here is the script:


VBSTART
Dim SQLString
set MyDB = CreateObject("ADODB.Connection")
' MyDB.CursorLocation = adUseServer
MyDB.Open "Provider=ASAProv;Data Source=C:\Database\sm_3.db;" & _
"User Id=dba;Password=sql;"

SQLString = "Select * from dba.system_settings where setting_name = 'SITE_ID'"
Set RS = MyDB

MyDB.Close
Set MyDB = nothing
VBEND

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Oct 25, 2007 9:14 pm

Set RS = MyDB.Execute(SQLString)

See:
http://www.mjtnet.com/vbsdb.htm
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

lebrocoli
Pro Scripter
Posts: 53
Joined: Tue Sep 06, 2005 9:28 pm

Post by lebrocoli » Fri Oct 26, 2007 7:22 pm

Ok great.

After shuffling the code around for some time it started to work for me.

Now, I guess the question is: from the 1 million site thats talk about VisualBasic, do you know of one that is simple and easy to learn from?

Thanks for all your answers.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 26, 2007 8:57 pm

Well you can narrow down your search - VB is NOT VBScript. So look at sites specific to VBScript not VB.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

lebrocoli
Pro Scripter
Posts: 53
Joined: Tue Sep 06, 2005 9:28 pm

Post by lebrocoli » Mon Oct 29, 2007 1:07 pm

Ok. Thanks.

lebrocoli
Pro Scripter
Posts: 53
Joined: Tue Sep 06, 2005 9:28 pm

Post by lebrocoli » Wed Oct 31, 2007 6:34 pm

Here is what is working right now. I do not understand that line:

GetCustomerName = claude.Fields("setting_value")

In fact, I understand the right part, not the left part. Why are we assigning the result of claude.Fields("setting_value") to GetCustomerName IF GetCustomerName IS the name of the function itself?

What a beginner question hein?

==============================================
Function GetCustomerName(guy)

Dim SQLString

set MyDB = CreateObject("ADODB.Connection")
MyDB.Open "Provider=ASAProv;Data Source=C:\Database\sm_3.db;" & _
"User Id=dba;Password=sql;"
SQLString = "select * from dba.system_settings where setting_name = 'SITE_ID'"
set claude = MyDB.Execute(SQLString)

GetCustomerName = claude.Fields("setting_value")

MyDB.Close
Set MyDB = nothing

End Function

VBEND
VBEval>GetCustomerName(guy),CustName
Message> %CustName%

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 31, 2007 7:04 pm

Why are we assigning the result of claude.Fields("setting_value") to GetCustomerName IF GetCustomerName IS the name of the function itself?
By assigning a value to the function name you are defining the RETURN value of the function. So in this case the function GetCustomerName RETURNS claude.Fields("setting_value"). Therefore when you CALL GetCustomerName (e.g. with the VBEval statement) the value of the function is returned. In this case the value of the function is the value of claude.Fields("setting_value")

The VBEval statement takes this form:

VBEval>VBScript_Statement,variable

The value returned by VBScript_Statement is put into variable. So if we do:

VBEval>GetCustomerName("1234"),theCustomerName

Then the value returned by the GetCustomerName function is put into the theCustomerName variable. The value returned by GetCustomerName is whatever is assigned to it - in your case claude.Fields("setting_value").

Does that make sense?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

lebrocoli
Pro Scripter
Posts: 53
Joined: Tue Sep 06, 2005 9:28 pm

Post by lebrocoli » Fri Nov 02, 2007 5:51 pm

Ok great. I still have a question though:

Assuming that GetCustomerName = claude.Fields("setting_value") sets the return value of the function, what is the role of the name between the parenthesis themselves of the Function line?

I mean for the following line, what is the role of (guy)?

Function GetCustomerName(guy)

I mean, I can change it to whatever and it still works. That is why I am confuse. Same thing happens with the VBEval statement. I could write
VBEval>GetCustomerName(joebloe) and it will still work! You see the confusion?

Is the name between the parenthesis is just there to say that only 1 parameter is passed to the function?

Thanks for all your help.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts