VB6 - Creating a Connection String based on text input - vba

Ok I have a connection String - But I want to write an app where the user can enter their own values from a text box and that will be used in the connection
ConnectionObj.Open _
"Provider = sqloledb;" & _
"Data Source=" & Text1.Text; & _
"Initial Catalog=" & text2.text; & _
"User ID= text3.text ;" & _
"Password= text5.text ;"
Here's what I've tried
But that didn't work - any help would be greate appreciated

Since I figured it out I thought I'd post it : if someone would need this in the future :)
ConnectionObj.Open _
("Provider = sqloledb;" & _
"Data Source=" & Text1.Text & _
";Initial Catalog=" & Text2.Text & _
";User ID=" & Text3.Text & _
";Password=" & Text4.Text & ";")
Just to make things absolutely clear the "&" is a concatenation operator "&_" denotes a continued (Long) line
Best Task'

Related

No value given for one or more required parameters error on one machine but not another

I have VBA code working in Excel on my machine but it is giving the above error on someone else's machine. What would be causing it to work for me but not for someone else?
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & wb.Path & "\" & wb.Name & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
'---Run the SQL SELECT Query---
sql = "SELECT " & _
"sum([" & cash_projections.Name & "$].[Projected Ending Balance]), " & _
"[" & cash_projections.Name & "$].[Account Name (Balance)], " & _
"[" & cash_projections.Name & "$].[Projected Date] " & _
"FROM " & _
"[" & cash_projections.Name & "$] " & _
"WHERE " & _
"[" & cash_projections.Name & "$].[Projected Ending Balance] > 100 " & _
"GROUP BY " & _
"[" & cash_projections.Name & "$].[Account Name (Balance)], " & _
"[" & cash_projections.Name & "$].[Projected Date] " & _
"ORDER BY " & _
"[" & cash_projections.Name & "$].[Account Name (Balance)], " & _
"[" & cash_projections.Name & "$].[Projected Date] "
Set rs = cn.Execute(sql)
The error shows up on the last line of this code. Again, it works just fine for me, but gives the error on another machine.
Please note that I am using the exact same workbook as is being used on the other computer. Here are the column headings
The line that is causing the error is the one that is executing your query. Because the code does not fail when you open the connection, it means it is finding the workbook you are querying just fine. My guess is that the workbook it finds has a different structure on the two machines. Because it says a parameter is missing a value, it means that something that is defined on your machine is missing on the other, it could be a table name or a column name. Basically, when you use that name on your machine it finds it and moves on. On the other machine, it can't find it so it assumes it must be parameter.

Edit/Update datagridview VB form

When I try to edit and update the data in datagriview it comes up with an error message saying Operator '&' is not defined for type 'TextBox' and string "".
please help. Thanks
Here is my code
Private Sub btnaddrecord_Click(sender As Object, e As EventArgs) Handles btnaddrecord.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
If Me.IdentificationNotest.Tag & "" = "" Then
cmd.CommandText = "INSERT INTO vehicledefects(Codenumber, vehiclereg, datereported, defects1, repaired1, defects2, repaired2, defects3, repaired3, datefixed) " & _
" VALUES(" & Me.IdentificationNotest.Text & ",'" & Me.vehiclereg.Text & "','" & Me.datereported.Text & "','" & Me.defects1.Text & "','" & Me.repaired1.Text & "','" & _
Me.defects2.Text & "','" & Me.repaired2.Text & "','" & _
Me.defects3.Text & "','" & Me.repaired3.Text & "','" & _
Me.datefixed.Text & "')"
cmd.ExecuteNonQuery()
Else
cmd.CommandText = "UPDATE vehicledefects" & _
" SET Codenumber =" & Me.IdentificationNotest.Text & _
", vehiclereg ='" & Me.vehiclereg.Text & "'" & _
", datereported ='" & Me.datereported.Text & "'" & _
", defects1 ='" & Me.defects1.Text & "'" & _
", repaired1 ='" & Me.repaired1.Text & "'" & _
", defects2 ='" & Me.defects2.Text & "'" & _
", repaired2='" & Me.repaired2.Text & "'" & _
", defects3='" & Me.defects3.Text & "'" & _
", repaired3='" & Me.repaired3.Text & "'" & _
", datefixed='" & Me.datefixed.Text & "'" & _
" WHERE Codenumber =" & Me.IdentificationNotest.Tag
cmd.ExecuteNonQuery()
End If
refreshdata()
Me.btnclear.PerformClick()
cnn.Close()
datefixed.Text = ""
IdentificationNotest.Text = ""
End Sub
In the future, you should also post the line number the error is being thrown on.
The error is telling you that you're doing something like:
dim myString as String = myTextBox & " some more text"
in this case, you would need to do:
dim myString as String = myTextBox.Text & " some more text"
In the code you posted, I wasn't able to find an instance of this - so perhaps its somewhere else in the code. Though, the code was hard to read so I may have missed it.
You may also be aware that this code is susceptible to SQL Injection attacks

How to specify "Service Name" in Oracle ODBC connection string

I have an Excel macro that has an Oracle database connection. When I call the macro, it fills out user name and password but not service name. User has to enter service name manually every time.
How can I specify it in the connection string?
Connection string:
CN.ODBCConnection.Connection = _
"ODBC;DRIVER={Oracle in OraClient11g_home2};" & _
"UID=" & inputUser & ";PWD=" & inputPassword & ";" & _
"HOST=" & inputHost & ";PORT=1521;DB=" & inputHost & ";" & _
"DefaultIsolationLevel=READUNCOMMITTED"
Excel prompt:
Found it. It's DBQ
New connection string:
CN.ODBCConnection.Connection = _
"ODBC;DRIVER={Oracle in OraClient11g_home2};" & _
"DBQ=" & inputHost & ";UID=" & inputUser & ";PWD=" & inputPassword & ";" & _
"HOST=" & inputHost & ";PORT=1521;DB=" & inputHost & ";" & _
"DefaultIsolationLevel=READUNCOMMITTED"
It depends on your driver, check this out
But without a config file, you have to specify the options in the connection string.
Give this a try,
CN.ODBCConnection.Connection = _
"ODBC;DRIVER={Oracle in OraClient11g_home2};" & _
"SERVICE_NAME=" & inputHost & ";UID=" & inputUser & ";PWD=" & inputPassword & ";" & _
"HOST=" & inputHost & ";PORT=1521;" & _
"DefaultIsolationLevel=READUNCOMMITTED"
Also, make sure inputHost should be used in both cases where it is currently. One should be an instance of a server with the other being the database within it.

vbscript that enters SQL and prints selected values to .txt file

I'm trying to connect to a SQL server(localhost) and extract the following:
SELECT INVOICENO, SUPPLIERID, AMOUNT, DOCID
FROM ES_TRANS_HEADER
WHERE READYTOINVOICE = 1
I then need to get this information into a .txt file, seperated by commas
This is what I got so far:
function accounting()
Dim myStream, connection, myCommand, recValue
Set myStream = CreateObject("ADODB.Stream")
Set connection = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command")
connection.Open "Provider=SQLNCLI10;" & _
"Data Source=localhost;" & _
"Integrated Security=SSPI;" & _
"Initial Catalog=SQLDatabase;" & _
"User ID=;Password="
myCommand.ActiveConnection=connection
myCommand.CommandText="SELECT INVOICENO,SUPPLIERID, AMOUNT, DOCID FROM ES_TRANS_HEADER WHERE READYTOINVOICE = 1"
SET recValue = myCommand.Execute()
If Not recValue.EOF then
MsgBox "INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1) _
& "AMOUNT=" & recValue(2) & "DOCID=" & recValue(3)
End If
While Not recValue.EOF
INVOICENO = recValue(0)
SUPPLIERID = recValue(1)
AMOUNT = recValue(2)
DOCCID = recValue(3)
recValue.MoveNext
Wend
end function
Am I on the right track here, and if so what should I try to do next?
Thanks in advance
MsgBox didn't have any parentheses missing. Remove them (and read Eric Lippert's most awesome blog post on parentheses in VBScript). What's missing in that line is a line continuation character (_) at the end of the line. In VBScript you cannot wrap lines without that. Change this:
MsgBox ("INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1)
& "AMOUNT=" & recValue(2) & "DOCID=" & recValue(3))
into this:
MsgBox "INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1) _
& "AMOUNT=" & recValue(2) & "DOCID=" & recValue(3)
or put the whole statement in a single line:
MsgBox "INVOICENO = " & recValue(0) & vbcrlf & "SUPPLIERID=" & recValue(1) & "AMOUNT=" & recValue(2) & "DOCID=" & recValue(3)
As for the database connection, your connection string seems odd. According to the information provided here that connection string is for connections to a mirrored database. Try this instead:
db = "..." 'specify database name here
connection.Open "Provider=SQLNCLI10;" _
& "Server=localhost;" _
& "Database=" & db & ";" _
& "Trusted_Connection=yes;"
If that doesn't help: check the value of the connection's State property:
WScript.Echo connection.State
Also, update your question with any error you're getting (error number, error message and line number).

Insert Variable Into SQL Query Visual Studio 2010

I have a SQL query that looks like this;
Dim query As String = "SELECT ID, " & Environment.NewLine & _
"month_of_sale, " & Environment.NewLine & _
"client_last_name, " & Environment.NewLine & _
"client_first_name, " & Environment.NewLine & _
"deal_number, " & Environment.NewLine & _
"stock_number, " & Environment.NewLine & _
"delivery_date, " & Environment.NewLine & _
"finance_manager, " & Environment.NewLine & _
"finance_income, " & Environment.NewLine & _
"record_entered " & Environment.NewLine & _
"FROM(dodge_records)" & Environment.NewLine & _
"WHERE month_of_sale = #month_of_sale AND client_last_name = #client_last_name" & Environment.NewLine & _
"ORDER BY client_last_name"
I am trying to insert a variable into the SQL query but I don't see it happening when I debug the website. Here is the rest of the code so that it makes more sense.
Case "Last Name"
Try
Dim query As String = "SELECT ID, " & Environment.NewLine & _
"month_of_sale, " & Environment.NewLine & _
"client_last_name, " & Environment.NewLine & _
"client_first_name, " & Environment.NewLine & _
"deal_number, " & Environment.NewLine & _
"stock_number, " & Environment.NewLine & _
"delivery_date, " & Environment.NewLine & _
"finance_manager, " & Environment.NewLine & _
"finance_income, " & Environment.NewLine & _
"record_entered " & Environment.NewLine & _
"FROM(dodge_records)" & Environment.NewLine & _
"WHERE month_of_sale = #month_of_sale" & Environment.NewLine & _
"WHERE client_last_name = #client_last_name" & Environment.NewLine & _
"ORDER BY client_last_name"
Using Connection = New MySqlConnection(connStr)
Using da = New MySqlDataAdapter(query, Connection)
da.SelectCommand.Parameters.AddWithValue("#month_of_sale", CurrentMonth)
da.SelectCommand.Parameters.AddWithValue("#client_last_name", ValueBoxText)
Dim DS As New DataSet
da.Fill(DS)
DodgeSearchGridView.DataSource = DS
DodgeSearchGridView.DataBind()
Connection.Close()
End Using
End Using
Catch ex As Exception
End Try
CurrentMonth & ValueBoxText are strings that I need inserted into the SQL query. Right now this isn't working how I want it to. I don't get an error message or anything, the query doesn't return anything and I have double checked the database to make sure there is data in it that would be returned by this query. Can anyone help me error check the code so I can find out how to make this work? Thanks!
Your SQL is incorrect. You have used WHERE twice. The second condition should use AND ie
AND client_last_name
Step through the code in the VS debugger and paste the query generated from the command into SQL Server Management studio and try it there.