Discussion Locked

How to execute SQL Script in VB?

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

how do i execute a sql scripte in VB6? the script looks like below:

=====================================
if exists (select * from dbo.sysobjects where id = object_id('[dbo].[rptBrandListing]') and OBJECTPROPERTY(id, 'IsView') = 1)
drop view [dbo].[rptBrandListing]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.[rptBrandListing]
AS
SELECT [tblBrand].[BrandID], [tblBrand].[BrandName]
FROM tblBrand

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
=======================================

seems that "DataEnvironment1.Connection1.Execute" is not capable in running a script which contains a key.

11 replies

by rolie

Try this one!!!

Create a variable of type database and a variable of type recordset.

i.e.
dim db as database
dim rs as recordset

Before you do this check if Microsoft DAO 3.51 Object library is supported. Go to Projects, then References. Check Microsoft DAO 3.51 Object Library.

set db = dbengine.opendatabase(...)
'Put the path of the database you want to access.
set rs = db.openrecordset(...)
'Put the SQL statement here. Please check for the parameters of this function

if you want to execute consecutive SQL statements.
check for the function QueryString in Recordset method.

by michealirwin

It sounds like you'll be wanting to do this more than once; consider making it into a stored procedure ...

Mike

by michealirwin

It sounds like you'll be wanting to do this more than once; consider making it into a stored procedure ...

Mike

by ohjani

Mike, yes i'll need to do it a few times, but i dun know how to pass the param into a stored procedure. this is what i've been thinking to put into a SP, but i dun think it will work:

CREATE PROCEDURE sp_ViewGeneration
@strViewName varchar(50),
@strViewDef varchar(500)
As
if exists (select * from dbo.sysobjects where id = object_id('[dbo].[@strViewName]') and OBJECTPROPERTY(id, 'IsView') = 1)
drop view [dbo].[@strViewName]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.[@strViewName]
AS
@strViewDef

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

by limbo

Jannie,

If you forward your email address to me (gskalley@postmater.com), I will send you a VB project I created, that demonstrates the core of what you need to know.

This discussion is closed to new replies.