Sub or Function is not defined vb6 - 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()"

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.

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

Related

VB6 Error :Object variable or with block variable not set

I get this
"Object variable or with block variable not set" inside 'dblog' sub function
in this line , I guess its with 'm_Session'
If iType >= Int(m_Session("_SysParam_LogLevel")) Then
My Code
Private m_Session As ASPTypeLibrary.Session
Public Function InitializeSite(Optional intCheckMode As Integer = 0) As Boolean
InitializeSite = False
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
DBLog "Initializing started - "
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = DBConnection
end with
end function
Public Sub DBLog(ByVal sTxt As String, Optional ByVal iType As Integer = 0, Optional ByVal sCategory As String = "DEBUG")
'On Error Resume Next
Dim cmd As ADODB.Command
If iType >= Int(m_Session("_SysParam_LogLevel")) Then
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = DBConnection
End With
End If
Set cmd = Nothing
On Error GoTo 0
End Sub
Property Get DBConnection() As String
DBConnection = IIf(Not IsNull(m_Session("_SysParam_DBConnection")), m_Session("_SysParam_DBConnection"), "")
End Property
Please help me moving forward.
Function InitializeSite should also initialize variable m_Session before calling DBLog sub, or you could modify code in DBLog sub adding
If m_Session Is Nothing Then Exit Sub
at the beginning.
Or you could initialize variable m_Session adding this sub to your class
Public Sub OnStartPage(SC As ASPTypeLibrary.ScriptingContext)
Set m_Session = SC.Session
End Sub
Take a look at How to share ASP classic session variable from ASP to VB6?
Try to replace your first line of code with:
Private m_Session As new ASPTypeLibrary.Session

userform vlookup in textbox

I am really new to VBA, was trying to play around with really basic things, userform and vlookup. Couldn't figure out vlookup error after many hours. Appreciate any input!
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdSend_Click()
' emailcommand Macro
'
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = emailaddress.Value
.Subject = Subjectbox.Value
.Body = "Hi, " & Fundname.Value & " is ready"
.Display
Application.SendKeys "%s"
End With
End Sub
Private Sub Fundnumber_Change()
Dim ws As Worksheet
Set ws = Sheets("Matrix")
With Me
.Fundname.Text = Application.VLookup(.Fundnumber.Text, ws.Range("A2:D141"), 4, False)
End With
End Sub

vb.net WMI query to string

I have a little issue with WMI query.
I have to check if a certain property exists in a WMI query instance, the code i have done is:
Imports System.Management
Imports System.Management.Instrumentation
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim search_cpu As New ManagementObjectSearcher("SELECT * FROM Win32_Processor")
Dim info_cpu As ManagementObject '= Convert.ToUInt32("search_cpu")
Dim cpu_v As Integer
For Each info_cpu In search_cpu.Get()
If search_cpu.Get("caption") = True Then
cpu_v = "Caption"
Label1.Text = ("Name: " & info_cpu(cpu_v).ToString())
End If
Next
End Sub
End Class
Any help will be appreciated.
Thanks in advance
I managed to reproduce. The below code works allright for me and Label1 now displays:
Intel64 Family 6 Model 58 Stepping 9
Sub Main()
Dim search_cpu As New ManagementObjectSearcher("SELECT * FROM Win32_Processor")
Dim info_cpu As ManagementObject '= Convert.ToUInt32("search_cpu")
Dim caption As String
For Each info_cpu In search_cpu.Get()
caption = info_cpu("caption").ToString()
Next
Label1.Text = caption
if (string.isnullorempty(caption))
Label1.Text ="<does not exist>"
End Sub

How to use class initialize vb6 to connect to SQL server?

Anyone can help me how can I do for that variable objusername and objpassword of sub login are recognized by the sub Class_Initialize ? I tried this but it does not work.
private cn as ADODB.Connection
private record As ADODB.Recordset
private objusername as variant
private objpassword as variant
Public Sub login(objuser As Variant, objpass As Variant)
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost\SQLExpress;Initial Catalog=bdd;User ID=" & objusername & ";Password=" & objpassword & ""
cn.Open
If cn.State = adStateOpen Then
MsgBox "welcome", vbOKOnly, "connexion"
End If
end sub
Private Sub Class_Initialize()
On Error GoTo erreur
Set cn = New ADODB.Connection
Set record = New ADODB.Recordset
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost\SQLExpress;Initial Catalog=bdd;User ID='" & objusername & "';Password='" & objpassword & "'"
cn.Open
Exit Sub
erreur:
If Err.Number = -2147217843 Then
MsgBox "connection failed"
End If
End Sub
and I call class like this but I have always an error.
Private Sub CmdOK_Click()
dim x as class1
set x = new class1
x.Login text1,text2
End Sub
How can I solve this.
Class_Initialize will execute when you call new class1, as that's before you call Login the code in Class_Initialize has no idea what the username and password are.
Connect in Login instead:
private cn as ADODB.Connection
private record As ADODB.Recordset
private objusername as variant
private objpassword as variant
Public Sub login(objuser As Variant, objpass As Variant)
objusername = objuser
objpassword = objpass
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost\SQLExpress;Initial Catalog=bdd;User ID=" & objusername & ";Password=" & objpassword & ""
cn.Open
...
end sub
Private Sub Class_Initialize()
End Sub
If you want to connect when you create an instance of Class1 use a factory function in a module which is the closest workaround for the lack of constructors.
public function CreateAndLogin(objuser As Variant, objpass As Variant) as class1
set CreateAndLogin= new Class1
CreateAndLogin.login bjuser, objpass
end function
called with
Dim cls as Class1
set cls = CreateAndLogin(text1, text2)

VBA Custom Event Not Found when Raised in UserForm

I was following this MSDN guide on creating custom events. I feel like I understand the process now, but I cannot figure out why I am getting a Compile Error: Event Not Found for RaiseEvent ItemAdded. The weird thing is, the ItemAdded event is recognized by the IDE (I can type it in all lowercase and it is then automatically formatted properly), so I know that it is recognized by VB.
DataComboBox Class Module Code:
Public Event ItemAdded(sItem As String, fCancel As Boolean)
Private pComboBox As Control
Public Property Set oComboBox(cControl As Control)
Set pComboBox = cControl
End Property
Public Property Get oComboBox() As Control
oComboBox = pComboBox
End Property
Private Sub Class_Initialize()
End Sub
Private Sub Class_Terminate()
End Sub
The UserForm contains two controls - a CommandButton named btnAdd and a ComboBox named cboData.
UserForm Code:
Private WithEvents mdcbCombo As DataComboBox
Private Sub UserForm_Initialize()
Set mdcbCombo = New DataComboBox
Set mdcbCombo.oComboBox = Me.cboData
End Sub
Private Sub mdcbCombo_ItemAdded(sItem As String, fCancel As Boolean)
Dim iItem As Long
If LenB(sItem) = 0 Then
fCancel = True
Exit Sub
End If
For iItem = 1 To Me.cboData.ListCount
If Me.cboData.List(iItem) = sItem Then
fCancel = True
Exit Sub
End If
Next iItem
End Sub
Private Sub btnAdd_Click()
Dim sItem As String
sItem = Me.cboData.Text
AddDataItem sItem
End Sub
Private Sub AddDataItem(sItem As String)
Dim fCancel As Boolean
fCancel = False
RaiseEvent ItemAdded(sItem, fCancel)
If Not fCancel Then Me.cboData.AddItem (sItem)
End Sub
You cannot raise an event outside the classes file level.
Add a routine like this inside "DataComboBox1" to allow you to raise the event externally.
Public Sub OnItemAdded(sItem As String, fCancel As Boolean)
RaiseEvent ItemAdded(sItem, fCancel)
End Sub
Then call the OnItemAdded with the current object.
Example...
Private WithEvents mdcbCombo As DataComboBox
...
mdcbCombo.OnItemAdded(sItem, fCancel)

Resources