Results 1 to 11 of 11

Thread: Why is this producing an error?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    I am trying to access a database in VB, so I have tried the following statement:

    Code:
    Dim cnn As New ADODB.connection
    Which gives the following error:

    "Compile Error: User-defined type not defined"

    This must be something very simple...but what?

    I've successfully established database connection in ASP using: ccn = Server.CreateObject("ADODB.connection")
    so I don't understand why the statement in question in invalid.

    Thanks for any help!

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Well, I don't work with databases, but if you say CreateObject works in ASP, why don't you try it in VB?
    Code:
    Dim cnn As Object
    Set cnn = CreateObject("ADODB.connection")

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Yonatan's approach will probably work, but the object variable will be late bound, and thus slower.
    To use:
    Dim cnn As New ADODB.connection
    you need to add a reference to the Microsoft ActiveX Data objects library. (Project --> References, check the checkbox and click OK)

    Success

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    i tried what you said frans, but i got the error:

    "Name conflicts with existing, module or object library."

    weird.

    yonatan's solution's seems to have worked...although like you said, will not perform as well.

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    If you also have a reference to DAo, this could be the problem. Both ADO and DAO have things like Recordset, connection and so on. If you use both, the types must be declared specific. So no more:
    dim rs as recordset
    but
    dim rs as ado.recordset
    or
    dim rs as dao.recordset

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    ok.. thanks frans.

    i was wondering if someone might be able to spot what is wrong with the following piece of code (from the same program i am writing)

    Code:
    Private Sub find_and_replace(pattern As String, data As String)
        Selection.Find.ClearFormatting     **
        Selection.Find.Text = pattern
        Selection.Find.Execute Forward:=True
        Clipboard.Clear
        Clipboard.SetText (data)
        Selection.Paste
        Clipboard.Clear
    End Sub
    
    
    Private Sub report_button_Click()
        Set ObjWord = New Word.Application
        report_button.Enabled = False
        Set doc1 = ObjWord.Documents.Open("c:\temp\report.doc")
        Call find_and_replace("building", "M-2")
    **gives me a runtime error 91: "Object Variable or with block variable not set"

    This was working fine this morning..!! I don't know what I changed?!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    Sorry, I mis-copied the above code block, here is the whole thing, which gives me the same error that I mentioned above:

    Code:
    Private Sub find_and_replace(pattern As String, data As String)
        Selection.Find.ClearFormatting
        Selection.Find.Text = pattern
        Selection.Find.Execute Forward:=True
        Clipboard.Clear
        Clipboard.SetText (data)
        Selection.Paste
        Clipboard.Clear
    End Sub
    
    
    Private Sub report_button_Click()
        Set ObjWord = New Word.Application
        report_button.Enabled = False
        Set doc1 = ObjWord.Documents.Open("c:\temp\report.doc")
        Call find_and_replace("building", "M-2")
        
              
        stmSQL = "SELECT * FROM NRCAeroLabStaff WHERE FirstName = 'Steve'"
       
       Set oRS = CreateObject("ADODB.Recordset")
       
       ' Open the connection.
       oRS.Open stmSQL, "DSN=labdata"
    
       oRS.MoveFirst
       
       Do While Not oRS.EOF
       Selection.TypeText Text:=oRS("FirstName")
       Selection.TypeParagraph
       Selection.TypeText Text:=oRS("LastName")
       Selection.TypeParagraph
       oRS.MoveNext
       Loop
    
       'Close the Recordset object.
       oRS.Close
              
              
              
        doc1.SaveAs FileName:="c:\temp\report1.doc"
        doc1.Close
    
    End Sub
    can anyone see a mistake?

    thanks!

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I think it has problems with the Selection object.
    Try:
    ObjWord.Selection.Find.ClearFormatting
    etc.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    how do i define the ObjWord globally, when my program simply consists of two subroutines..?

    I tried passing the ObjWord as an argument to the find_and_replace sub, but that didn't work...

  10. #10
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Did you pass it ByRef as Word.Application?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    yeah i think so....

    i just deleted that sub()
    it wasnt crucial. things seem to work ok now.
    thanks for the help.

    btw, would you happen to know the following:

    once I have this VB program completely written, and saved as a standard .exe is there an easy way to run it from ASP? or do I have to change all the code to VBScript?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width