?
Solved

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

Posted on 2001-06-20
4
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 900 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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