jhovyn
jhovyn

Reputation: 265

Sub or Function is not defined vb6

Why am I getting an error "Sub or Function is not defined"...Here is my code

FORM2

Option Explicit
Public Report As New CrystalReport1
Public mvCn As New ADODB.Connection

 Public Function printReport()

 Dim strConnectionString As String
 Dim rs As ADODB.Recordset
 Dim strScript As String

 strConnectionString = "Provider=SQLOLEDB............"

 mvCn.ConnectionString = strConnectionString
 mvCn.CommandTimeout = 0
 mvCn.CursorLocation = adUseClient
 mvCn.Open

 strScript = strScript & "SELECT * FROM employee" & vbCrLf

 Set rs = mvCn.Execute(strScript)

 Report.Database.SetDataSource rs
 Report.AutoSetUnboundFieldSource crBMTNameAndValue

 CRViewer1.ReportSource = Report
 CRViewer1.ViewReport

 Set Report = Nothing

 End Function

Form 1.....Call my function "printReport" here

Option Explicit

Private Sub Command1_Click()

printReport

End Sub

The error message goes here "Private Sub Command1_Click()"

Upvotes: 1

Views: 2588

Answers (2)

Yogesh Waran
Yogesh Waran

Reputation: 31

A procedure can be called in such a simple way.[As you have called is correct]

Eg.

Private Sub Form_Load()
    Test1
End Sub

Sub Test1()
    MsgBox "Test1"
End Sub

Upvotes: -1

MarkL
MarkL

Reputation: 1771

Where is your printReport function defined? If it's in a class module, then you need to instantiate an instance of the class then call printReport as a method of that class. For instance:

Private Sub Command1_Click()
  Dim oClass As New Class1

  oClass.printReport

End Sub

Or you can place your printReport function in a module, then you don't instantiate it or call it as a method - you would instead call it as you have in your click event.

Upvotes: 3

Related Questions