Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 8th, 2003, 11:20 PM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to use VBA to Read/Write a Access Query

Hi, everybody:
I wish to use VBA to read/write a Exist Query's sql text in Access. Do anyone can give me some help and point out how to do this.

Thanks for your help.

For example:
1. First, I create a Query in Access, and save it as "qryCustom"
2, Now, I wish to read the Query "qryCustom" SQL Text in VBA code, then I can modify the SQL Text for other purpose.
3, If need, I wish I can update the original Query with new SQL Text.

Pls help! Thanks.

Best Regards,
Gino Liu at CI.
 
Old September 8th, 2003, 11:31 PM
Authorized User
 
Join Date: Jun 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this code....Be sure to set a reference to DAO.

    Dim qdefTemp As DAO.QueryDef

    'Change the querydef
    Set qdefTemp = CurrentDb.QueryDefs("qselCustom1")
    qdefTemp.SQL = "SELECT * FROM newTable - Put your SQL here"
    qdefTemp.Close
    Set qdefTemp = Nothing


HTH,
Mike
 
Old January 7th, 2012, 04:03 AM
Registered User
 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default this works

first create a SP in your SQL database using


USE [yentel]
GO
/****** Object: StoredProcedure [dbo].[__ListQuery] Script Date: 01/07/2012 08:37:11 ******/
/****** Author: Frans Eilering ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
create PROCEDURE [dbo].[__ListQuery]
AS
BEGIN
SET NOCOUNT ON;
select obj.Name as SPName,
modu.definition as SPDefinition,
obj.create_date as SPCreationDate
FROM sys.sql_modules modu
INNER JOIN sys.objects obj
ON modu.object_id = obj.object_id
--you may also insert a where clause to select only SP beginning with a letter
--WHERE obj.type = 'P' and left(obj.Name,1)='B'
End




Sub test()

'then execute this vba piece of code

Dim cn As ADODB.Connection
Set cn = CurrentProject.AccessConnection

Set rs1 = New ADODB.Recordset
With rs1
Set .ActiveConnection = cn
.Source = "__ListQuery"
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open
End With
rs1.MoveFirst
With rs1
While Not .EOF
'loop through all queries
SPName = !SPName
contents = !SPDefinition
'do what ever you want with the name and the contents
rs1.MoveNext
Wend
End With
End Sub

Last edited by frans eilering; January 7th, 2012 at 04:13 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Query from Access to MySAL using VBA stephej Access 3 June 20th, 2006 06:56 AM
How to read/write to Access database from Visual C mmakrzem Access 1 July 30th, 2005 01:57 AM
Access VBA Delete Query Issue... snoopy92211 Access VBA 4 June 24th, 2004 03:57 PM
How to read/write Access Database jmmendes Access VBA 0 November 18th, 2003 01:26 PM
Logging File Access Read & Write ntbluez VBScript 1 November 13th, 2003 12:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.