|
-
Sep 9th, 2000, 09:59 AM
#1
Thread Starter
Member
Hi,
Can anyone help me with an SQL problem.
Do you know if your able to include multiple lines of SQL in VB?
And are you allowed to define SQL variables in VB or do all statments have to begin with SELECT.
Thank you very much for all your help
Sajjad
-
Sep 9th, 2000, 10:21 AM
#2
Hyperactive Member
I didn't fully understand the question ..
but you can make any SQL statment in VB like..
select ... from ... where ...
insert into .... (fields) values (....)
delete from .... where ....
practically you can do all the things that you can do with the SQL analyzer (you send it to SQL server anyway).
In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.
- Douglas Adams
The Hitchhiker's Guide to the Galaxy
-
Sep 9th, 2000, 10:39 AM
#3
Thread Starter
Member
Thanx for replying,
What I am asking is:
Are you able to add multiple line of SQL code in VB or are there any special parameters or syntax that you must include in VB to run a multi-line SQL querie.
For insance:
SELECT * FROM table
WHERE ID = (SELECT ID FROM othertable WHERE NAME = 'TEXT1.TEXT')
The above statment is on multiple lines, if you get me.
Also I need to declare variables in my program. And was wondering can you declare SQL variables in VB or do your statments all have to begin with SELECT, DELETE UPDATE. For instance:
DECLARE @Hello
SELECT @HELLO = ID FROM sometable WHERE ID = (SELECT ID FROM othertable WHERE NAME = 'TEXT1.TEXT')
If you dont undertand me then please email me @ [email protected]
Thank you for all you help...once again
Sajjad
-
Sep 9th, 2000, 10:50 AM
#4
_______
<?>
same rules as vb
the seperator is the underscore _
Code:
Sub ClientServerX2()
Dim dbsCurrent As Database
Dim qdfBestSellers As QueryDef
Dim qdfBonusEarners As QueryDef
Dim rstTopSeller As Recordset
Dim rstBonusRecipients As Recordset
Dim strAuthorList As String
' Open a database from which QueryDef objects can be
' created.
Set dbsCurrent = OpenDatabase("DB1.mdb")
' Create a temporary QueryDef object to retrieve
' data from a Microsoft SQL Server database.
Set qdfBestSellers = dbsCurrent.CreateQueryDef("")
With qdfBestSellers
.Connect = "ODBC;DATABASE=pubs;UID=sa;PWD=;" & _
"DSN=Publishers"
.SQL = "SELECT title, title_id FROM titles " & _
"ORDER BY ytd_sales DESC"
Set rstTopSeller = .OpenRecordset()
rstTopSeller.MoveFirst
End With
' Create a temporary QueryDef to retrieve data from
' a Microsoft SQL Server database based on the results from
' the first query.
Set qdfBonusEarners = dbsCurrent.CreateQueryDef("")
With qdfBonusEarners
.Connect = "ODBC;DATABASE=pubs;UID=sa;PWD=;" & _
"DSN=Publishers"
.SQL = "SELECT * FROM titleauthor " & _
"WHERE title_id = '" & _
rstTopSeller!title_id & "'"
Set rstBonusRecipients = .OpenRecordset()
End With
' Build the output string.
With rstBonusRecipients
Do While Not .EOF
strAuthorList = strAuthorList & " " & _
!au_id & ": $" & (10 * !royaltyper) & vbCr
.MoveNext
Loop
End With
' Display results.
MsgBox "Please send a check to the following " & _
"authors in the amounts shown:" & vbCr & _
strAuthorList & "for outstanding sales of " & _
rstTopSeller!Title & "."
rstTopSeller.Close
dbsCurrent.Close
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Sep 9th, 2000, 12:04 PM
#5
Thread Starter
Member
Hi,
Thanx for all your replies...
Bye the way I'm not using Microsoft SQL server, just accessing a Access datababase (.mdb)
However, I'm still having problems with the SQL variable.
Can you define it as:
Dim DB as Database
Dim RS as Recorset
Set DB = OpenRecordset ("C:\myData.mdb)
Set RS = DB.OpenRecordset("DECLARE @ID" & _
"SELECT @ID = (SELECT age FROM directory" & _
"WHERE name = 'Text1.Text")
Set RS = DB.OpenRecordset ("SELECT * FROM listing" & _
"WHERE ID = @ID")
Can you check my code, and check that you can define variables in the way I have, and that Text1.Text has the correct sintax around it.
Thank you very much
Sajjad
-
Sep 9th, 2000, 12:39 PM
#6
Monday Morning Lunatic
I don't think Access supports the DECLARE statement. It would probably be easier to make it all into one large SQL statement:
Code:
Dim DB as Database
Dim RS as Recordset
Set DB = OpenDatabase("C:\myData.mdb")
Set RS = DB.OpenRecordset("SELECT * FROM listing WHERE ID = (SELECT age FROM directory WHERE name = " & Text1.Text & ")")
Don't know if this will work, but it's the sort of thing.
[Edited by parksie on 09-09-2000 at 03:49 PM]
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 9th, 2000, 02:46 PM
#7
Thread Starter
Member
Thank you very much for everybodies input. I am VERY greatful.
I would also appreciate any other ideas from anyone else.
Thanx
Sajjad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|