Solved

View is not useable after Appending (Access, ADOX, VB)

Posted on 2001-06-20
4
143 Views
Last Modified: 2010-05-02
Running Win '98, Access 97, VB

I'm using the included Function to create views.

Problem 1)
After creating a view, I open Access and check it out.
It look ok, but reports an error when executed.
After touching the SQL (inserting a blank, replacing an 'a' by an 'a') the view works perfect. ?

Problem 2)
Normal views can be access by the following code, but not the view created by the function I include.

dim ViewSql as string
    ....
    ....
    Set DBcmd = dbcat.Views(ViewName).Command
   
    ViewSQL = Trim(DBcmd.CommandText)

Can anybody help?
Am I missing a Property somewhere?

-------------------
Private Function ViewCreate(ViewName As String, ViewSQL As String) As Boolean
Dim dbcat As ADOX.Catalog
Dim DBcmd As ADODB.Command
       
' create view (xxx)

    ViewCreate = False
   
    On Error GoTo ErrorHandling
   
    Set dbcat = New ADOX.Catalog
    Set dbcat.ActiveConnection = ConnectionString
    Set DBcmd = New ADODB.Command

    DBcmd.CommandText = ViewSQL
   
    dbcat.Views.Append ViewName, DBcmd
    dbcat.Views.Refresh
   
    Set dbcat = Nothing
    Set DBcmd = Nothing
   
    ViewCreate = True
   
    Exit Function

ErrorHandling:
    MsgBox ERR.Number & " " & ERR.Description
   
End Function
0
Comment
Question by:abekke
  • 2
4 Comments
 
LVL 1

Expert Comment

by:mariab_
ID: 6210737
Hi,

try to create the view this way:

where ViewSQL is for example: "CREATE VIEW titles_view AS SELECT title, type, price, pubdate FROM titles"

Private Function ViewCreate(ViewName As String, ViewSQL As String) As Boolean

Dim DBcmd As ADODB.Command
       
' create view (xxx)

   ViewCreate = False
   
   On Error GoTo ErrorHandling

   Set DBcmd.ActiveConnection = ConnectionString

   DBcmd.CommandText = ViewSQL
   DBcmd.Execute ViewSQL
   
   Set DBcmd = Nothing
   
   ViewCreate = True
   
   Exit Function

ErrorHandling:
   MsgBox ERR.Number & " " & ERR.Description
   
End Function
0
 
LVL 1

Expert Comment

by:mariab_
ID: 6210744
ops!

without the line "DBcmd.CommandText = ViewSQL" :)))

Regards :))
0
 
LVL 5

Accepted Solution

by:
ianouii earned 300 total points
ID: 6213153
> Problem 1)
> After creating a view, I open Access and check it out.
> It look ok, but reports an error when executed.
> After touching the SQL (inserting a blank, replacing
> an 'a' by an 'a') the view works perfect. ?

it's most likely when u create your view, the string that is being passed in isn't correct. try to check on the view string to ensure it's ok & workable when execute. U can use Query Analyzer to help u.

> Problem 2)
> Normal views can be access by the following code, but
> not the view created by the function I include.

This might bcoz of your view error. if your view isn't working properly, when invoke it, ofcoz u'll encounter error. try to resolve your 1st problem, I'm sure your 2nd problem can be easily resolved.

good luck.
0
 

Author Comment

by:abekke
ID: 6224498
The Sql contained an error!

This is what I found out:
----- if I use

  DBcmd.commandText = ViewSql
  DBcat.Views.Append ViewName, DBcmd
  DBcat.Views.Refresh

The view is created even if it contains an error

------ while using
  DBcmd.CommandText = "CREATE VIEW " & ViewName & " AS " & ViewSql
  DBcmd.Execute

The View is validated

Thanks to both of you!

This is the code I ended up with:
-------------------
Private Function ViewCreate(ViewName As String, ViewSQL As String) As Boolean
Dim DBcmd As ADODB.Command
Dim TotalViewSql As String

    ViewCreate = False
   
    On Error GoTo ErrorHandling
   
    Set DBcmd = New ADODB.Command
    Set DBcmd.ActiveConnection = ConnectionString
   
    TotalViewSql = "CREATE VIEW " & ViewName & " AS " & ViewSQL
    DBcmd.CommandText = TotalViewSql
    DBcmd.Execute
   
    Set DBcmd = Nothing
   
    ViewCreate = True
   
    Exit Function

ErrorHandling:
    MsgBox ERR.Number & " " & ERR.Description
   
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now