How to return the value from function - vb6

How to return the value from function
Code
Private Function LeaveCheck(empid As String)
Dim rdoRs1 As rdoResultset
Dim desc As String
Dim sSQL As String
sSQL = "Select name from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
desc = rdors1!name
return desc 'Showing error in this line
End If
rdoRs1.Close
End Function
How to return the value from the above code.
Need Vb6 code Help

You need to specify your return type.
Private Function LeaveCheck(empid As String) As String ' Notice the As String
Dim rdoRs1 As rdoResultset
Dim desc As String
Dim sSQL As String
sSQL = "Select name from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
desc = rdors1!name
End If
rdoRs1.Close
LeaveCheck = desc ' This will be blank or populated
End Function
Here is a link that is a good read for understanding Functions in VB6
EDIT
After reading your comment, I would create a class to store your values.
Public Class MyClass
Dim name As String
Dim dept As String
Dim country As String
End Class
Then, you can instantiate a new instance of this class inside of your code:
Private Function LeaveCheck(empid As String) As MyClass
Dim myClass As New MyClass
Dim rdoRs1 As rdoResultset
Dim sSQL As String
sSQL = "Select name, dept, country from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
myClass.name = rdors1!name
myClass.dept = rdors1!dept
myClass.country = rdors1!country
End If
rdoRs1.Close
LeaveCheck = myClass
End Function

You need to set the value to the function name and the return type:
Private Function LeaveCheck(empid As String) As String
Dim rdoRs1 As rdoResultset
Dim desc As String
Dim sSQL As String
sSQL = "Select name from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
desc = rdors1!name
End If
rdoRs1.Close
LeaveCheck = desc
End Function
See this document for more information.

Function returnArray() As Variant
RTA[ab]=0
if a=b then RTA[ab]=1
RTA[xy]=0
if a=b then RTA[xy]=1
returnArray=RTA
end function

Related

System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression

I have made lines of code as seen :
`
Public Class frm_login_a188500
Private Sub btn_login_Click(sender As Object, e As EventArgs) Handles btn_login.Click
Dim mysql As String = "SELECT COUNT( * ) AS NUM_MATCHES FROM TBL_USERS_A188500 WHERE FLD_USERNAME ='" & txt_username.Text & "’ and FLD_PASSWORD ='" &
txt_password.Text & "’"
Dim mydatatable As New DataTable
Dim myreader As New OleDb.OleDbDataAdapter(mysql, myconnection)
myreader.Fill(mydatatable)
Dim num_matches As String = mydatatable.Rows(0).Item("NUM_MATCHES")
If num_matches = 1 Then
frm_mainmenu_a188500.Show()
Me.Hide()
Else
txt_username.Text = ""
txt_password.Text = ""
MsgBox("Incorrect Username or Password")
End If
End Sub
End Class
`
However, there's an error :
System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression 'FLD_USERNAME ='’ and FLD_PASSWORD ='’'.'
Expecting to get the error so that I can fix it.
Don't use smart quotes:
Dim mysql As String = "SELECT COUNT(*) AS NUM_MATCHES FROM TBL_USERS_A188500 WHERE FLD_USERNAME ='" & txt_username.Text & "' And FLD_PASSWORD ='" & txt_password.Text & "'"

display data in list view format after query from databases

I added a filter method which is filter by department by select the combo box options, however, I have no idea about how to push the data into the list after query from database. Below is my code.
Private Sub comboDept_Click()
Dim sQuery As String
Dim oRS As New ADODB.Recordset
Dim oRS_PR As New ADODB.Recordset
Dim sPONO As String
Dim sPOAmt As String
combVal = comboDept.List(comboDept.ListIndex)
If combVal = "EIBU_SALES" Then
sQuery = "Select PO_No, PO_Requestor, PO_Req_Dept, PO_Status, PO_Approval_M, PO_Approval_GM, PO_Approval_D, PO_HRApproval, VC_No, TH_Sup_Inv, PO_HR_Rmk, PO_Req_Date, PO_SupplierName, PO_OverallAmt from PR_INFO where PO_Req_Dept = '" & combVal & "'"
oRS_PR.Open sQuery, PRCnn, adOpenDynamic, adLockOptimistic
ElseIf comboDept.List(comboDept.ListIndex) = "MCBU_SALES" Then
Try something like this (it's just a hint; you have to adjust the code):
' Empty list
myListView.ListItems.Clear
' Add items
While (Not oRS_PR.EOF)
Set item = myListView.ListItems.Add(, , oRS_PR!FIRST_COLUMN)
item.SubItems(...) = oRS_PR!SOME_COLUMN
item.SubItems(...) = oRS_PR!OTHER_COLUMN
oRS_PR.MoveNext
Wend

Outlook mail message into excel using VBA

Excel File Link
This is a code from an you-tube video. The below code is giving an Compiler error : Userdefined Type not defined.
Sub SendEmail(what_address As String, Subject_line As String, mail_body As String)
'Dim olApp As Outlook.Application
Set olApp = CreateObject("outlook.Application")
'Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = what_address
olMail.Subject = Subject_line
olMail.body = mial_body
olMail.send
End Sub
Sub SendMassEmail()
row_number = 1
Do
DoEvents
row_number = row_number + 1
Dim mail_body_message As String
Dim full_name As String
Dim Promoscode As String
mail_body_message = Sheet1.Range("J2")
full_name = Sheet1.Range("B" & row_number) & " " & Sheet1.Range("C" & row_number)
promo_code = Sheet1.Range("D" & row_number)
mial_body_message = Replace(mail_body_message, "replace_name_here", full_name)
Call SendEmail(Sheet1.Range("A1" & row_number), "This is a test e-mail", mail_body_message)
Loop Until row_number = 6
End Sub
I am having an compiler error, I have checked everything but...not sure what is cousing this issue.
Use the Recipients property of the MailItem class to specify the recipients instead of the To property.
The Recipients class provides the Add method which allows to create and add a new recipient to the collection. Then use the Resolve method to attempt to resolve a Recipient object against the Address Book.
Sub AssignTask()
Dim myItem As Outlook.TaskItem
Dim myDelegate As Outlook.Recipient
Set MyItem = Application.CreateItem(olTaskItem)
MyItem.Assign
Set myDelegate = MyItem.Recipients.Add("DL name")
myDelegate.Resolve
If myDelegate.Resolved Then
myItem.Subject = "Prepare Agenda For Meeting"
myItem.DueDate = Now + 30
myItem.Display
myItem.Send
End If
End Sub

VB6.0 with DataControl Database Programming

can you help out access the database... I have been reading some tutorials but I don't know where to start doing this one. I used DataControl to access the database. First, the program will prompt for the ID Number and then Search for the further information and display it in texboxes when Search Employee button clicked. I know how to set the properties of textboxes in order to appear the value of my database to my output without clicking the Search Employee button but I want to click first the button Search Employee. I'm a beginner in VB6. Please help me out! I need this project now.
Ok, I had some time to spare, here you go, first add a reference to Microsoft ActiveX Data Objects 2.X Library:
Form1 Code:
Option Explicit
''Add the following items to your form and name them as indicated:
''Four(4) text boxes - Named: tbIDNumber, tbName, tbAddress, and tbContactName.
''One(1) Command button - Named Command1
Private Sub Command1_Click()
Dim rs As ADODB.Recordset
Dim DB As cDatabase
Dim l As Long
Set rs = New ADODB.Recordset
Set DB = New cDatabase
With DB
.DBCursorType = adOpenForwardOnly
.DBLockType = adLockReadOnly
.DBOptions = adCmdText
.DSNName = "Your_DSN_Name"
.SQLUserID = "Your_SQL_Login_Name"
.SQLPassword = "Your_SQL_Login_Password"
Set rs = .GetRS("Select Name, Address, ContactNumber FROM YourTableName WHERE IDNumber = '" & tbIDNumber.Text & "'")
End With
If rs.RecordCount > 0 Then
tbName.Text = rs(0).Value & ""
tbAddress.Text = rs(1).Value & ""
tbContactName.Text = rs(2).Value & ""
End If
Exit_Sub:
rs.Close
Set rs = Nothing
Set DB = Nothing
End Sub
Add a Class Module Object to your project and name it cDatabase. Then copy the following Code into it:
Option Explicit
Private m_eDBCursorType As ADODB.CursorTypeEnum 'Cursor (Dynamic, Forward Only, Keyset, Static)
Private m_eDBLockType As ADODB.LockTypeEnum 'Locks (BatchOptimistic,Optimistic,Pessimistic, Read Only)
Private m_eDBOptions As ADODB.CommandTypeEnum 'DB Options
Private m_sDSNName As String
Private m_sSQLUserID As String
Private m_sSQLPassword As String
Private cn As ADODB.Connection
Private Sub Class_Initialize()
m_eDBCursorType = adOpenForwardOnly
m_eDBLockType = adLockReadOnly
m_eDBOptions = adCmdText
End Sub
Private Function ConnectionString() As String
ConnectionString = "DSN=" & m_sDSNName & "" & _
";UID=" & m_sSQLUserID & _
";PWD=" & m_sSQLPassword & ";"
''If you are using MS Access as your back end you will need to change the connection string to the following:
''ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
''If you are using a DNS-Less connection to SQL Server, then you will need to change the connection string to the following:
''ConnectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=" & m_sSQLUserID & ";Password=" & m_sSQLPassword & ";"
''You can find more Connection Strings at http://connectionstrings.com/
End Function
Private Sub GetCN()
On Error GoTo GetCN_Error
If cn.State = 0 Then
StartCN:
Set cn = New ADODB.Connection
cn.Open ConnectionString
With cn
.CommandTimeout = 0
.CursorLocation = adUseClient
End With
End If
On Error GoTo 0
Exit Sub
GetCN_Error:
If Err.Number = 91 Then
Resume StartCN
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetCN of Module modDatabaseConnections"
End If
End Sub
Public Function GetRS(sSQL As String) As ADODB.Recordset
Dim eRS As ADODB.Recordset
On Error GoTo GetRS_Error
TryAgain:
If Len(Trim(sSQL)) > 0 Then
Call GetCN
Set eRS = New ADODB.Recordset 'Creates record set
eRS.Open sSQL, cn, m_eDBCursorType, m_eDBLockType, m_eDBOptions
Set GetRS = eRS
Else
MsgBox "You have to submit a SQL String"
End If
On Error GoTo 0
Exit Function
GetRS_Error:
If Err.Number = 91 Then
Call GetCN
GoTo TryAgain
ElseIf Err.Number = -2147217900 Then
Exit Function
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetRS of Module" & vbCrLf & vbCrLf & "SQL - " & sSQL
End If
End Function
Public Property Get DBOptions() As ADODB.CommandTypeEnum
DBOptions = m_eDBOptions
End Property
Public Property Let DBOptions(ByVal eDBOptions As ADODB.CommandTypeEnum)
m_eDBOptions = eDBOptions
End Property
Public Property Get DBCursorType() As ADODB.CursorTypeEnum
DBCursorType = m_eDBCursorType
End Property
Public Property Let DBCursorType(ByVal eDBCursorType As ADODB.CursorTypeEnum)
m_eDBCursorType = eDBCursorType
End Property
Public Property Get DBLockType() As ADODB.LockTypeEnum
DBLockType = m_eDBLockType
End Property
Public Property Let DBLockType(ByVal eDBLockType As ADODB.LockTypeEnum)
m_eDBLockType = eDBLockType
End Property
Public Property Get DSNName() As String
DSNName = m_sDSNName
End Property
Public Property Let DSNName(ByVal sDSNName As String)
m_sDSNName = sDSNName
End Property
Public Property Get SQLUserID() As String
SQLUserID = m_sSQLUserID
End Property
Public Property Let SQLUserID(ByVal sSQLUserID As String)
m_sSQLUserID = sSQLUserID
End Property
Public Property Get SQLPassword() As String
SQLPassword = m_sSQLPassword
End Property
Public Property Let SQLPassword(ByVal sSQLPassword As String)
m_sSQLPassword = sSQLPassword
End Property
This should do the trick.

Issue related record set sheet in database connectivity

Please find below the code..
Function Connect_to_db(Byval mfgprt)
Dim cnn,rss
Set cnn = CreateObject("ADODB.Connection")
Set rss = CreateObject("ADODB.recordset")
cnn.ConnectionString = "DSN=QTPDSN;Description=desc;UID=;PWD=;APP=QuickTest Professional;WSID=;DATABASE=;"
cnn.open
rss = cnn.Execute (""select UnitPrice from ProductProfilePrices where MfPartNumber ='" + mfgprt + "'")
Connect_to_db=rss(0)
End Function
In this function, if I change a col name unit price in Query with '*' then it will return more than one value..in that case how to use rss .....
As if i'll do it(replace unit price with '*'),then while running it populates an error..in rss data fetching..
please by doing same modify the code.....
Thanks,
Galstar
You can refer to the fields by name, but first rss should be an object, so use Set, also the string concatenator is & :
Set rss = cnn.Execute (""select UnitPrice, Quantity " _
& " from ProductProfilePrices where MfPartNumber ='" & mfgprt & "'")
''Let us say that only one row is returned for mfgprt :
varUnitPrice = rss("UnitPrice")
varQuantity = rss("Quantity")
EDIT re comments
Connect_to_db "AAA", Val1, Val2
MsgBox Val1 & " " & Val2
Function Connect_to_db(ByVal mfgprt, ByRef Val1, ByRef Val2)
Dim cnn, rss
Set cnn = CreateObject("ADODB.Connection")
Set rss = CreateObject("ADODB.recordset")
cnn.ConnectionString = "DSN=QTPDSN;Description=desc;" _
& "UID=;PWD=;APP=QuickTest Professional;WSID=;DATABASE=;"
cnn.Open
rss = cnn.Execute("select UnitPrice, Quantity " _
& " from ProductProfilePrices where MfPartNumber ='" & mfgprt & "'")
Val1 = rss(0)
Val2 = rss(1)
End Function

Resources