Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default To SQL Expert: VBA Function that Calls SQL Query

Hi everyone,

I have a VBA function like this:

Function Total(VarName1, VarTable, VarName2, VarValue1, VarName3, VarValue2)

Inside the function, I have a query of the type:
Total = "Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND VarName3 = 'VarValue2' "

The function will be used as Add-ins in Excel. Note the words that start with "Var..". In a cell, I would type: "=Total(..,..,..,..,..,..)"

My question: How should I define Var.. ones in "Function Total(...)" and "Total = " so those values are typed inside the excel cell, and then scrolled down?

Thanks,
Mike
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default To SQL Expert: VBA Function that Calls SQL Query

Hi Mike

I'm quite sure I don't understand your question. But anyway, this will never
work:

"Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND VarName3 =
'VarValue2' "

Everything between this pair " " is treated as literally this, and your
search for text 'VarValue1' will probably return nothing. Notation with
variables should read:

"Select VarName1 From VarTable Where VarName2 = '" & VarValue1 & "' AND
VarName3 = '" & VarValue2 & "'"

Best wishes Harald


"Mike" skrev i melding
...
Hi everyone,

I have a VBA function like this:

Function Total(VarName1, VarTable, VarName2, VarValue1, VarName3,
VarValue2)

Inside the function, I have a query of the type:
Total = "Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND
VarName3 = 'VarValue2' "

The function will be used as Add-ins in Excel. Note the words that start
with "Var..". In a cell, I would type: "=Total(..,..,..,..,..,..)"

My question: How should I define Var.. ones in "Function Total(...)" and
"Total = " so those values are typed inside the excel cell, and then
scrolled down?

Thanks,
Mike



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default To SQL Expert: VBA Function that Calls SQL Query

On Wednesday, November 20, 2013 5:32:36 PM UTC-5, Harald Staff wrote:
Hi Mike I'm quite sure I don't understand your question. But anyway, this will never work: "Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND VarName3 = 'VarValue2' " Everything between this pair " " is treated as literally this, and your search for text 'VarValue1' will probably return nothing. Notation with variables should read: "Select VarName1 From VarTable Where VarName2 = '" & VarValue1 & "' AND VarName3 = '" & VarValue2 & "'" Best wishes Harald "Mike" skrev i melding ... Hi everyone, I have a VBA function like this: Function Total(VarName1, VarTable, VarName2, VarValue1, VarName3, VarValue2) Inside the function, I have a query of the type: Total = "Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND VarName3 = 'VarValue2' " The function will be used as Add-ins in Excel. Note the words that start with "Var..". In a cell, I would type: "=Total(..,..,..,..,..,..)" My question: How should I define Var.. ones in "Function Total(...)" and "Total = " so those values are typed inside the excel cell, and then scrolled down? Thanks, Mike


Thanks Harald...
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default To SQL Expert: VBA Function that Calls SQL Query

On Thursday, 21 November 2013 14:19:20 UTC, Mike wrote:
On Wednesday, November 20, 2013 5:32:36 PM UTC-5, Harald Staff wrote:

Hi Mike I'm quite sure I don't understand your question. But anyway, this will never work: "Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND VarName3 = 'VarValue2' " Everything between this pair " " is treated as literally this, and your search for text 'VarValue1' will probably return nothing. Notation with variables should read: "Select VarName1 From VarTable Where VarName2 = '" & VarValue1 & "' AND VarName3 = '" & VarValue2 & "'" Best wishes Harald "Mike" skrev i melding ... Hi everyone, I have a VBA function like this: Function Total(VarName1, VarTable, VarName2, VarValue1, VarName3, VarValue2) Inside the function, I have a query of the type: Total = "Select VarName1 From VarTable Where VarName2 = 'VarValue1' AND VarName3 = 'VarValue2' " The function will be used as Add-ins in Excel. Note the words that start with "Var..".. In a cell, I would type: "=Total(..,..,..,..,..,..)" My question: How should I define Var.. ones in "Function Total(...)" and "Total = " so those values are typed inside the excel cell, and then scrolled down? Thanks, Mike




Thanks Harald...


You should also make sure you deal with the possibility of quotes within
the VarValues (to prevent errors and SQL injection attacks), so:

"Select VarName1 From VarTable Where VarName2 = '" & Replace(VarValue1,"'","''") & "' AND
VarName3 = '" & Replace(VarValue2,"'","''") & "'"
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with a function that calls itself vsoler Excel Programming 3 September 13th 09 01:29 PM
STATISTIC FUNCTION USING EXCEL.. I NEED AN EXPERT Moh Excel Worksheet Functions 1 September 10th 06 05:52 AM
Using ODBC function calls Dave[_64_] Excel Programming 0 February 15th 06 02:37 PM
COUNT function and errors within it - Need some expert knowledge stu eade Excel Discussion (Misc queries) 2 February 1st 05 01:07 AM
function calls Claude Excel Programming 2 December 5th 03 01:55 PM


All times are GMT +1. The time now is 06:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"