DataGrid in VB6 - vb6

I am working on a VB6 project that uses a MS Access database. I want to display the value of a column in DataGrid using the following logic:
1 for a value of 1
2 for a value of 2
0 for other values
Can anyone help me?

You can add the logic directly into your SQL statement using IIF statement:
Select IIF(YourValue < 2, YourValue, 0) as NewValue, * FROM YourTable
This code loads records from a YourTable table (please update with your table name) and applies the logic your described to the YourValue field (if value is smaller than 2, return 0):
Dim objConnection As ADODB.Connection
Dim objRecordset As New ADODB.Recordset
Dim sSQLStatement As String
Set objConnection = New ADODB.Connection
Set objRecordset = New ADODB.Recordset
sSQLStatement = "Select IIF(YourValue < 2, YourValue, 0) as NewValue, * FROM YourTable"
objConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\YourDatabase.mdb"
objConnection.Open
objRecordset.CursorLocation = adUseClient
objRecordset.Open sSQLStatement, objConnection, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = objRecordset
You can add this logic to more columns, you simply need to add extra IIF statments to your sSQLStatement string:
Select IIF(Value1 < 2, Value1, 0) as NewValue1, IIF(Value2 < 2, Value2, 0) as NewValue2, IIF(Value3 < 2, Value3, 0) as NewValue3 FROM YourTable

Related

How to run an insert query on VB 6.0?

I am New to Visual Basic 6.0 and I don't know how to code SQL queries, specially Insert Into
I have 1 database with 2 tables(tblNames, tblRemarks)..
in tblNames the fields are:
ID, LastName, FirstName, MidName, DateHired, Position, Department
in tblRemarks the fields are:
ID, FullName, txtDate, Remarks, DateHired, Position
What I am doing is like this:
SQL = "SELECT ID, LastName, FirstName, MidName, DateHired, Position FROM tblNames"
SQL2 = "SELECT * fROM tblRemarks"
and then on my form I have a DTPicker1 and Command1
I want to get all the records in tblNames and put it on tblRemarks, but also get the date from DTPicker1 and in Fields Remarks, the word "HOLIDAY".
What I'm doing is like this: om my Module I have this code:
Option Explicit
Public conn As New ADODB.Connection
Public RS As New ADODB.Recordset
Public cmd As New ADODB.Command
Public SQL As String
Public SQL2 As String
Public Sub connectDB()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "\Database.accdb;User ID=admin;Persist Security Info=False;JET OLEDB:Database Password=qqqq"
conn.Open
With RS
.ActiveConnection = conn
.Open SQL, conn, 3, 3
End With
End Sub
Public Sub connOpen()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "\Database.accdb;User ID=admin;Persist Security Info=False;JET OLEDB:Database Password=qqqq"
conn.Open
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = SQL
Set RS = .Execute
End With
End Sub
Then I just call that in my forms
my problem is this, I have 2 SQL queries, how can I connect these 2 SQL in my connection? I researched it, and found out I can do 1 QUERY by using INSERT.
I tried, but I cannot make it work. How can I achieve this? I am very new to VB6. Would it be like this?
SQL = "INSERT ID, LastName, FirstName, MidName, DateHired, Position FROM tblNames" _
& "INTO tblRemarks, #" & DatePick & "# as txtDate, 'HOLIDAY' as Remarks"
Totally untested but perhaps you can try the following
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "\Database.accdb;User ID=admin;Persist Security Info=False;JET OLEDB:Database Password=qqqq"
conn.Open
SQL = "INSERT INTO tblRemarks " & _
"(FullName, txtDate, Remarks, DateHired, Position) " & _
"SELECT CONCAT_WS(' ', FirstName, MidName, LastName), ?, 'HOLIDAY', DateHired, Position " & _
"FROM tblNames"
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = SQL
.Parameters.Append .CreateParameter(, adVarWChar, adParamInput, 100, DatePick)
.Execute
End With
Replace 100 with the width of your txtDate column.

populate values in textbox if select combobox vb6

I am using VB 6.0 I have one form (Form1), 1 combobox (ComboBox1), and 1 textbox (TextBox1)I have one table (Salary) in my local database which was created within the project.In Table 'Salary' I only have Four columns (UserID - Primary Key, Salary Type, Salary Range)the table has multiple records in it.
What I need to find out is how do have get the textbox to populate with the corresponding columns for whatever is selected in the combobox. Any and all help would be appreciated.
Here is the code which i was used to link database with VB :
Private WithEvents cmdPopulate As CommandButton
Private WithEvents dcbDataCombo As DataCombo
Private Sub Form_Load()
Dim rs As ADODB.Recordset
Dim strConnect As String
Dim strSQL As String
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mahmoud\Desktop\Project\Database.mdb;Persist Security Info=False"
strSQL = "Select Distinct * FROM Salary order by UserID asc" ' set ascending order
Set rs = New ADODB.Recordset
With rs
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open Source:=strSQL, _
ActiveConnection:=strConnect, _
Options:=adCmdText
End With
Set DataCombo1.RowSource = rs
DataCombo1.ListField = "UserID"
DataCombo1.DataField = "UserID"
End Sub
You wrote:
ActiveConnection:=strConnect
but strConnect is a string, while ActiveConnection require a ADODB.Connection object, not a string. Moreover, the connection must be already Open:
Dim CN As ADODB.Connection
Set CN = New ADODB.Connection
CN.ConnectionString = strConnect
CN.Open
CN.CursorLocation = adUseClient
' for recordset:
rs.Open strSQL, CN, adOpenStatic, adLockReadOnly, adCmdText
I have updated my answer from just the click event to a complete example:
Option Explicit
Private WithEvents cmdPopulate As CommandButton
Private WithEvents dcbDataCombo As DataCombo
Private rs As ADODB.Recordset
Private Sub Form_Load()
Dim strConnect As String
Dim strSQL As String
Dim cn As ADODB.Connection
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mahmoud\Desktop\Project\Database.mdb;Persist Security Info=False"
strSQL = "Select Distinct * FROM Salary order by UserID asc" ' set ascending order
Set cn = New ADODB.Connection
cn.ConnectionString = strConnect
cn.Open
cn.CursorLocation = adUseClient
Set rs = New ADODB.Recordset
rs.Open strSQL, cn, adOpenStatic, adLockReadOnly, adCmdText
Set DataCombo1.RowSource = rs
DataCombo1.ListField = "UserID"
DataCombo1.DataField = "UserID"
End Sub
Private Sub DataCombo1_Click(Area As Integer)
rs.MoveFirst
rs.Find "UserID = " & DataCombo1.Text
If Not (rs.BOF Or rs.EOF) Then TextBox1.Text = rs.Fields("SalaryType").Value
End Sub
If the field name SalaryType is not correct then fix as needed.

Fetching Column with table name alias is not working in VBA

I am trying to connect to Oracle table through VBA and fetching data. My situation is to reference one table multiple times to do pivot. But while running query via VBA it's throwing an error as I am fetching data through table alias: Below is a dummy code.
Sub Button1_Click()
Dim strDataSource, strUsername, strPassword, strConnectionString, strquery As String
Dim objADODBConnection, objADODBRecordset As Object
strDataSource = "xxxx"
strUsername = "xxxx"
strPassword = "xxxx"
strConnectionString = "Provider=MSDAORA;Data Source=" & strDataSource & ";Persist Security Info=True;Password=" & strPassword & ";User ID=" & strUsername
Set objADODBConnection = CreateObject("ADODB.Connection")
objADODBConnection.Open strConnectionString
Set objADODBRecordset = CreateObject("ADODB.Recordset")
strquery = "SELECT T1.Column1 FROM Table1 AS T1 WHERE Column2='XXXX' AND Column3='XXXX'"
Set objADODBRecordset = objADODBConnection.Execute(strquery)
MsgBox objADODBRecordset.Fields(0).Value
objADODBRecordset.Close
objADODBConnection.Close
Set objADODBRecordset = Nothing
Set objADODBConnection = Nothing
End Sub
The same query is running fine without table alias. Please suggest!!
FROM Table1 AS T1
You should remove 'AS' in from condition -
FROM Table1 T1

Error '80004005' only when SELECT DISTINCT with ASP Classic

I am developing in ASP VBScript at work and need to run a SELECT DISTINCT query but I am having some troubles.
I have other queries in my code that work perfectly fine, that do not use SELECT DISTINCT.
Here is what I am using:
Dim sections()
c = 1
set conn=Server.CreateObject("ADODB.Connection")
set rs=Server.CreateObject("ADODB.Recordset")
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=******;User ID=Admin;" & _
"DATA SOURCE=" & Server.MapPath("modules.mdb")
rs.open "SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC",conn
ReDim sections(10)
do while not rs.EOF
sections(c) = rs("section")
c = c + 1
rs.MoveNext
loop
rs.Close
conn.Close
set rs = nothing
set conn = nothing
Which gives me this error:
error '80004005'
on the line of the SQL query
The only way to fix this is to use "GROUP BY" instead of "DISTINCT"
SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC
SELECT section FROM modules WHERE area='First' GROUP BY section ORDER BY lvl ASC

When I select the value from the combo box, related value should appear in the text box

When I select the value from the combo box, related value should appear in the text box
ComboBox code.
cmd.CommandText = "select distinct PERSONID from T_PERSON"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("PersonID")) Then
txtno.AddItem rs("PersonID")
End If
rs.MoveNext
Wend
In comboBox list of ID is displaying, when I select the particular person id, Name should display in text box related to the personid
Text Box
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("Name")) Then
txtName.Text = rs("Name")
rs.MoveNext
End If
Wend
I put the above code in Form_Load Event, Nothing displaying in Text Box.
What wrong in my code.
Need VB6 code Help
You would want the 2nd block of code in the the click event for the combobox.
Edit
There looks like another couple of issues in your code at this line:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
2 Issues:
You are passing in the control itself as the person ID, not the selected value.
You have an extra space in your query after the person ID
You should change that line to be:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
Why not have the combobox display the name and hold the personID as it's item data?
cmd.CommandText = "select distinct PERSONID, Name from T_PERSON WHERE PersonID IS NOT NULL"
Set rs = cmd.Execute
While Not rs.EOF
combo.AddItem rs("Name").value
combo.ItemData(combo.NewIndex) = rs("PERSONID").value
rs.MoveNext
Wend
Then, if you need the PersonID for the selected name you can just grab combo.ItemData(combo.ListIndex).

Resources