Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 131,719 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,318 people online right now. Registration is fast and FREE... Join Now!




Sql Queries Using Adodc Control?

 
Reply to this topicStart new topic

Sql Queries Using Adodc Control?, SQL through RecorsSource?

born2c0de
post 5 Feb, 2005 - 11:38 AM
Post #1


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


I've seen code that uses SQL Queries by placing the query as:
CODE

Data1.RecordSource="select * from people;"


But this is for the Data Control when used with DBGrid and DataGrid Controls. But how can SQL Queries be used on an ADODC Control?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 5 Feb, 2005 - 02:17 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


You can use adodc with DataGrid controls, although I'm sure you knew that...did you not want to use DataGrid controls at all?
CODE

Private Sub Command1_Click()
  Dim sql As String
  ‘define SQL query
  sql = "select * from staff;"
  ‘specify format of SQL result and assisgn to ADODC
  Adodc1.CommandType = adCmdText
  Adodc1.RecordSource = sql
  Adodc1.Refresh
  ‘Display result in the datagrid object
  Set DataGrid1.DataSource = Adodc1
End Sub

Can't remember where that example is from, I had it in my code snippets.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 5 Feb, 2005 - 08:21 PM
Post #3


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Ahh...I just wanted that one line of code
CODE

Adodc1.CommandType = adCmdText

I kept mine to adCmdTable and maybe that's why It wasn't working in the DataGrid control...thanx again
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 5 Feb, 2005 - 08:42 PM
Post #4


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


oh...yeah...i also figured out why it wasnt working....the sql queries arent supposed to contain the ';' character...thanx
User is offlineProfile CardPM

Go to the top of the page

moongamb
post 9 Feb, 2005 - 07:15 AM
Post #5


New D.I.C Head

*
Joined: 9 Feb, 2005
Posts: 1

I'm kind of new to this and not sure if I can phrase the question correctly. But on this same topic:

I am using a ADO Control with a DataGrid an I am wondering how and if I can code the SQL Command Text under the ADOdc1 properties page record source so that I can use a variable from a VB form in the SQL code to limit the records returned?

Example:

My variable is STATE_NAME (which = ca)

And I would like the code to be something like:

Select * from TABLE_NAME where STATE_FIELD = STATE_NAME

The Adodc1 SQL doesn’t seem to accept the variable STATE-NAME ?

any help will be appreciated……thanks moongamb
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 9 Feb, 2005 - 10:06 AM
Post #6


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


I guess u can do it this way...i'm not too sure:
CODE

Adodc1.RecordSource="Select * from TABLE_NAME where STATE_FIELD ="+ STATE_NAME


if STATE_NAME is not a String then you can use str$(STATE_NAME) in the code shown above.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 9 Feb, 2005 - 06:06 PM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


born2C0de's method will work for you...if for some reason it does not...

First, verify that the variable STATE_NAME holds the value you want, then you can try the following:
CODE

Adodc1.RecordSource="Select * from TABLE_NAME where STATE_FIELD ='"& STATE_NAME & "'"

Please note that there is a single quote before the double quotes after the equal sign, and another single quote between the sets of closing double quotes. The concatenation operator is the ampersand. If indeed the value of STATE_NAME is 'ca', the code above will yield the following:
QUOTE

Adodc1.RecordSource="Select * from TABLE_NAME where STATE_FIELD ='ca'
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 10 Feb, 2005 - 02:58 AM
Post #8


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


yea...and remember....DONT INCLUDE SEMICOLONS INTO THE QUERY
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 10:03AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month