Fetching Column with table name alias is not working in VBA - oracle

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

Related

saving input in a csv file VB script

Below script capture in a raw text file. But I want to save input in a csv file into cell. Kindly help
Dim employee_id, employee_name, system_running_status
employee_id = InputBox("Enter your Employee ID", "Employee ID")
employee_name = InputBox("Enter your Name", "Employee Name")
fission_running_status = InputBox("Did you Powered-On the system today (Yes/No) ?","Fission Server Status")
Set obj = CreateObject("Scripting.FileSystemObject")
Const ForWriting = 8
Set obj1 = obj.OpenTextFile("aap.txt", ForWriting)
obj1.WriteLine ("Employee: " & employee_name & "-" & employee_id & " turned on system on: ") & Now() & vbCr
obj1.Close
Set obj=Nothing
You can simply reformat your output string:
Set obj1 = obj.OpenTextFile("aap.csv", ForWriting)
obj1.WriteLine employee_name & "," & employee_id & "," & Now() & vbCr

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.

DataGrid in 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

VBA "INSERT INTO" error - Importing data from Excel

I'm using a VBA code in excel to pick some data and insert it into an Access DB. I need this for several tables.
The problem is: For some of those tables, I get an error like: "INSERT INTO syntax error". But, if I get the string the code generates and uses for inserting, and use it in SQL mode form Access, the query works just fine. So that doesn't make any sense. Here is a piece of it:
For j = 6 To lastrow
SQLStr = "INSERT INTO TENSILE(REFERENCE, REF_ID, POSITION, RATIO, YSL0, YSL90, YSL180, YSL270, YST0, YST90, YST180, YST270, UTSL0, UTSL90, UTSL180, UTSL270, UTST0, UTST90, UTST180, UTST270, EL0, EL90, EL180, EL270, ET0, ET90, ET180, ET270, ARL0, ARL90, ARL180, ARL270, ART0, ART90, ART180, ART270) SELECT '" & ws3.Cells(j, 1) & "', REF.ID,'" & ws3.Cells(j, 60).Value & "'"
For i = 61 To 93
SQLStr = SQLStr & "," & ws3.Cells(j, i).Value
Next i
SQLStr = SQLStr & " FROM REF WHERE REF.REFERENCE LIKE '" & ws3.Cells(j, 1) & "'"
ws3.Cells(7, 3).Value = SQLStr
MsgBox (SQLStr)
'rs.Open SQLStr, con, adOpenStatic, adLockOptimistic 'Opening the query
Next j
it's important to notice that this same structure is used for other tables and works normaly, like in:
For j = 6 To lastrow
SQLStr = "INSERT INTO METALOGRAPHY(REFERENCE, REF_ID, AUSTGRAINSIZE) SELECT '" & ws3.Cells(j, 1) & "',REF.ID ," & ws3.Cells(j, 18).Value & " FROM REF WHERE REF.REFERENCE LIKE '" & ws3.Cells(j, 1) & "'"
'MsgBox (SQLStr)
'ws3.Cells(2, 3).Value = SQLStr
rs.Open SQLStr, con, adOpenStatic, adLockOptimistic 'Opening the query
Next j
What is going wrong?
Have you considered changing how you are linked to the Access DB so that you could use a statement like:
db.execute SQLStr
This should solve your problem

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.

Resources