Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what's wrong with this code? 1

Status
Not open for further replies.

torturedmind

Programmer
Jan 31, 2002
1,052
PH
good day peopl. am not really a vb programmer tho i understand a bit so please bear with me.

i have the following code i copied from the faq section and modified it a little to my need. problem is it throws me an error on the line indicated which states "Compile error. Invalid use of New keyword."

Code:
Private Sub btn_inv_chk_Click()
    Dim mydata As DataObject
    Set mydata = New DataObject

    [COLOR=red][b]' The following line is where the error shows[/b][/color]
    Set db_data = New Connection

    db_data.CursorLocation = adUseClient

    [COLOR=green]' set up DSN-less connection
    ' db_data.Open "Driver={Microsoft dBASE Driver (*.dbf)};" & _
    '    "DriverID=277;" & _
    '    "Dbq=c:\windows\desktop"[/color]

    db_data.Open "Driver={Microsoft Visual FoxPro Driver};" & _
        "SourceType=DBF;" & _
        "SourceDB=c:\windows\desktop;" & _
        "Exclusive=No"

    Set Ado_data = New Recordset

    [COLOR=green]]' Note exactly the same select statement as would be used in a relational database
    '[/color]
    Ado_data.Open "select MIS.matr_no, MIS.qty from MIS.dbf MIS", _
        adOpenStatic, adLockOptimistic

    If Ado_data.RecordCount < 1 Then
        MsgBox "No data found!"
        Exit Sub
    End If

    [COLOR=green]' go to the first record in the set and loop around till no more available
    '[/color]
    Ado_data.MoveFirst
    For i = 0 To Ado_data.RecordCount - 1
        ' MsgBox (Ado_data.Fields.Item(0).Value & " " & Ado_data.Fields.Item(1).Value)
        db_block = db_block & Ado_data.Fields(0).Value & vbTab & Ado_data.Fields(1).Value & vbCrLf
        Ado_data.MoveNext
    Next

    mydata.SetText Mid(data_block, 1)
    mydata.PutInClipboard
    Cells(1, 1).Select
    ActiveSheet.Paste

    Ado_data.Close
    db_data.Close
End Sub

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
You need to dim the variable as a connection object before you can set it (I think)

The Microsoft mascot is a butterfly.
A butterfly is a bug.
Think about it....
 
Have you referenced the Microsoft ActiveX Data Objects 2.x Library ?
Dim db_data As New ADODB.Connection

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
After toying around with this for a while, including settting/unsetting references, declaring variables in certain ways, and being unable to duplicate the compiler error, I searched MSDN. The only result returned was this KB article (and an identical one for XL2002):


You may want to check this out and see if it applies.


Regards,
Mike
 
thank you all for the suggestions. sir mike, your link was really helpful as it did solved the compiler error message. just after that workaround, another error popped up from the ffg. line of code:
Code:
    Ado_data.Open "select MIS.matr_no, MIS.qty from MIS.dbf MIS", _
        adOpenStatic, adLockOptimistic
any ideas?

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
You need to include the Connection object in the Recordset Open method:
Code:
Ado_data.Open "select MIS.matr_no, MIS.qty from MIS.dbf MIS", _
        [b]db_data[/b], adOpenStatic, adLockOptimistic


Mike
 
ei thatnks sir mike. you've been very very helpful indeed. for that i give a a star.

peace! [peace]

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top