Good afternoon everyone.
Having a problem trying to get my head around understanding if you can connect to the same database but select a different catalogue using a ComboBox1.
I have a CommandButton_Click() that when the user selects this this will make the connection to the right catalogue that has been selected which is Car1. This then outputs the sql query to the worksheet.
The first catalogue is called CAR1 and the second catalogue is called CAR2 connecting to the same databse called DATABASE1.
Is there a way that when the user selects car2 using the combobox and then presses the command button this will connect to the correct catalogue? Can a warning text box display a message to say that the wrong catalogue has been selected?
Any help would be appreciated.
Having a problem trying to get my head around understanding if you can connect to the same database but select a different catalogue using a ComboBox1.
I have a CommandButton_Click() that when the user selects this this will make the connection to the right catalogue that has been selected which is Car1. This then outputs the sql query to the worksheet.
Code:
Private Sub CommandButton1_Click()
'Declare variables'
Dim objMyConn As ADODB.Connection
Dim objMyCmd As ADODB.Command
Dim objMyRecordset As ADODB.Recordset
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=DATABSE1;Initial Catalog=CAR_Info;Integrated Security=SSPI;"
objMyConn.ConnectionTimeout = 0
objMyConn.CommandTimeout = 0
objMyConn.Open
'Set and Excecute SQL Command'
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandText = " SELECT * from SQLDATASET "
objMyCmd.CommandType = adCmdText
'Open Recordset'
Set objMyRecordset.Source = objMyCmd
objMyRecordset.Open
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset objMyRecordset
End Sub
The first catalogue is called CAR1 and the second catalogue is called CAR2 connecting to the same databse called DATABASE1.
Code:
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "Car1"
.AddItem "Car2"
End With
End Sub
Is there a way that when the user selects car2 using the combobox and then presses the command button this will connect to the correct catalogue? Can a warning text box display a message to say that the wrong catalogue has been selected?
Any help would be appreciated.