473,809 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass a public variable to a sql query

347 Contributor
I have a public variable that I declared in form1 of an application. I am trying to call that variable in form2 and then pass that variable in a sql query.

If I declare

Public Class Form1
Public payPeriodStartD ate, payPeriodEndDat e As Date

How then to I declare that variable in form2 and how to I pass it to my sql query

Expand|Select|Wrap|Line Numbers
  1. SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime) as duration INTO ScratchPad2
  2.  
  3. FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber
  4. where [Exceptions].exceptiondate between [payPeriodStartDate] and [payPeriodEndDate]
  5.  
  6. GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime,
  7. [Exceptions].code, [Exceptions].exceptiondate
  8.  
(The bold sections is where I need to call the public variables in my query)

thank you

Doug
Nov 4 '10 #1
13 5899
marcellus7
33 New Member
Are you using a tableadapter? That makes it a lot easier. From the tableadapter you can add the query, and when creating the query use an @ to declare parameters. So you'd create teh query like this in the tableadapter:
Expand|Select|Wrap|Line Numbers
  1. SELECT name, address
  2. FROM Users
  3. WHERE name = @name
  4.  
And then you'd call the query from the tableadapter in your code like this:

Expand|Select|Wrap|Line Numbers
  1. UsersTableAdapter.NameQuery(Form1.name)
  2.  
Where you call the query with your variable as a parameter.
Nov 5 '10 #2
dougancil
347 Contributor
Actually now that I think about this ... I currently have a datagrid view in my form and it has just a

Select * from exceptions query. I declare these variables at the start of my form:

Public payPeriodStartD ate As Date
Public payPeriodEndDat e As Date

and what I'd like to do is to modify my query for the datagrid view to be something more like this

select * from exceptions
where exceptiondate between payperiodstartd ate and payperiodenddat e.

What's the best way to do that?

Thank you
Nov 5 '10 #3
marcellus7
33 New Member
Ok, what you can do then is there is an option in the query wizard, when you create the query in the tableadapter, to use the query to fill a datatable. So declare the datatable in the Form (not in the OnClick event), and in the onclick event run the query to fill the datatable. Then you can just set the DataGrid source to the newly filled datatable.
Nov 5 '10 #4
dougancil
347 Contributor
Marcellus,

Does your answer apply to my modified question as well?

thank you,

Doug
Nov 5 '10 #5
marcellus7
33 New Member
Sorry I had to re-read your answer to grasp it. The best way to do that is to create a DataView, set the DataView source to the source table that holds the data for your SELECT * FROM Exceptions query, and filter the DataView based on those dates. In this scenario your DataGrid source would be the dataview. Heres an example:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2. Dim dataTable as New DataTable
  3. Dim dataView as New DataView
  4.  
  5. dataTable.Fill(SELECT * From Exceptions) 'Just an example of wherever you're filling the datatable
  6.  
  7. DataView.Table = dataTable
  8. DataGrid.Source = DataView
  9. DataView.RowFilter = "ExceptionDate > '" & PayPeriodStartDate & "' AND ExceptionDate < '" & PayPeriodEndDate  & "'"
  10.  
It would be something along those lines..
Nov 5 '10 #6
dougancil
347 Contributor
Marcellus,

Wouldnt it also be possible then in the datagridview to alter the query that's in there to reflect

select * from exceptions
where exceptiondate between Payperiodstartd ate and PayperiodEnddat e?
Nov 5 '10 #7
marcellus7
33 New Member
How are you storing the data thats being displayed in the DataGridView? DataGridViews are usually used only to display information thats being stored in a table somewhere. Can you post the code where you select the data and display it in the datagrid?
Nov 5 '10 #8
dougancil
347 Contributor
This is the only code I have for the datagridview, because I have the SQL connection bound into the form.

SELECT Employeenumber, Exceptiondate, Starttime, Endtime, Duration, Code, Approvedby FROM dbo.Exceptions
Nov 5 '10 #9
dougancil
347 Contributor
Marcellus,

If I just wanted to use the datagridview wizard and create a query to display data between payrollstartdat e and payrollenddate, how would I construct that query in the wizard? Basically, the data isn't being modified in this "initial" datagrid view. This is only for display. There will be a form that will allow users to edit from this form that will store this data back to the database.

this is the query that I'm trying to execute in my datagridview:
Expand|Select|Wrap|Line Numbers
  1. SELECT        Employeenumber, Exceptiondate, Starttime, Endtime, Duration, Code, Approvedby
  2. FROM            Exceptions
  3. where [Exceptions].exceptiondate between '" & payperiodStartDate & "' and '" & payPeriodEndDate"
  4.  
but I'm getting an error that it's unable to parse the query. Can you offer me a suggestion of how to fix this.

Thanks

Doug
Nov 9 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1676
by: Sunil Korah | last post by:
I have declared 'db;ProgID' as a public variable in a module I have a form with a combo box in which I choose a particular programme and the value gets stored as dblProgID. I know this part works correctly, because MsgBox(dblProgID) at this point displays the correct value. I then open a report for which the following query (saved query) is the record source.
2
1729
by: THY | last post by:
Hi, I am having some problem, I declare few variable in a public module and use them in the web application. But after that I found that the variable declared in public variable = application("variable"), and it will be shared by all user ... but I am not very sure about it, anyone know it ? Thanks, Tee
2
2055
by: aparnasinha26 | last post by:
Hi All, I have to pass variable in a query string. The query string is in the hyperlink coloumn of a datgrid. The code appears like this <asp:HyperLinkColumn DataNavigateUrlField="ProductID" DataNavigateUrlFormatString="ViewProduct.aspx?qProductID='<%strProduct%>'" DataTextField="ProductID" HeaderText="Product ID"> <HeaderStyle Width="10%"></HeaderStyle> </asp:HyperLinkColumn>
3
30115
by: Tor Inge Rislaa | last post by:
How to use a public variable In VB.6.0 I could in the declaration part of a form declare a public variable, then assign a value to the variable, open a new form and address the variable and read it's value in my new form. Form1:
0
1207
by: CES | last post by:
All, I was wondering is their a way of passing a variable into an eval() statement? I've created a custom eval() dll to use with my VB.net code. Every thing works fine until I try to pass a variable into the text string . In the code below I'm trying to pass the variable zz into txtFS which comes from the file file.txt. Any suggestions on how to get this to work would be appreciated. CES
3
1990
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on the WEB site to mail to passing a variable from the WEB site to the ASPX web site to E-Mail to? Hope I explained this correctly. This is a response from another group. There was no way for you to know it, but this is a classic asp newsgroup....
1
1686
by: I.am.the.Buddha | last post by:
I am still learning asp and sql. I am having trouble with passing a variable to within an sql statement. I am sure it is something simple like misuse of quotes. Since it may help if i say what i am trying to generally accomplish here. This is an access database for products and customers. We have a counter for each product (such as ProdCountLB, ProdCountSH, etc for the field names in a table called ProdCount). This worked fine when i...
3
1632
by: dsalex | last post by:
I have a form that has a subform to ask/assign a value (inserted by the user) to a public variable (VB). The same form has an Option Group that lauches named querys using DoCmd.OpenQuery. How can I pass the value held in my public variable to the query as a parameter ? The query has a "where" clause that should "understand" the value inserted by the user on my form. Can anybody help me ?
11
1944
by: dgk | last post by:
If I have a class with a public variable, isn't this the same as a private variable with a property? ie, isn't this: Public Class MyTest Public MyVar as String End Class the same as this: Public Class MyTest
2
7274
by: stanlen | last post by:
I have created a form using Access 2000. I declare a public variable Public lngStartDate as long I set the variable to 20070101. I then open a report based on a query. The criteria for the queria is ...WHERE iDate = Forms!frmMyForm.lngStartDate. The report opens fine and I get the expected data.
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.