Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
abhay_547
Starting Member
1 Post |
Posted - 2010-05-22 : 03:18:36
|
| Hi All,I have created a excel userform which pulls the data from sql server table on the basis of the parameters selected on the userform.Following are the details of the same.1) Mydata - Table which contains the data which I want to pull.2) Country_Region Mapping - Table to populate listbox1 with country column from this table and used as a criteria in sql statment for pulling data from Mydata table.3) Cost Element Mapping - Table to populate listbox2 with FSI_LINE3_code column from this table and used as a criteria in sql statment for pulling data from Mydata table.4) Cost Center Mapping - Table to populate listbox4 with Sub Product UBR Code column from this table and used as a criteria in sql statment for pulling data from Mydata table.Following is the code which I have as of now. Basically there are three joins required in the sql statement.Join 1 : [CODE]mydata.[Company Code] = [country_Region mapping].[Company Code] 'Join Type : INNER JOIN [/CODE]Join 2 : [CODE] mydata.[Cost Center] = [Cost Center Mapping].[Cost Center]'Join Type : INNER JOIN [/CODE]Join 3 : [CODE] mydata.[Unique Indetifier 1] = [Cost Element Mapping].[CE_SR_NO]'Join Type : INNER JOIN [/CODE]Below is the code which I have so far in my Userform background which works fine with the first join. I have bolded \ highlighted the sql statment in which I need help to incorporate other two joins and get the data from mydata on the basis of that :[CODE]Private Sub CommandButton5_Click()Dim selection As String' Get the selected products escaping single quotes'selection = Replace(UserForm2.listbox4.Value, "'", "''")Dim lItem As LongFor lItem = 0 To ListBox4.ListCount - 1If ListBox4.Selected(lItem) = True Thenselection = selection & "'" & Replace(ListBox4.List(lItem), "'", "''") & "',"End IfNextselection = Mid(selection, 1, Len(selection) - 1)' Setup connection stringDim connStr As StringconnStr = "Provider=SQLOLEDB.1;Password=adminL;User ID=*****;Integrated Security=SSPI;" _& "Persist Security Info=True;Initial Catalog=XXXXXX;" _& "Data Source=XXXXXXXX"' Setup the connection to the databaseDim connection As ADODB.connectionSet connection = New ADODB.connectionconnection.ConnectionString = connStr' Open the connectionconnection.Open' Open recordset.Set Cmd1 = New ADODB.CommandCmd1.ActiveConnection = connectionCmd1.CommandText = "SELECT * FROM dbo.mydata t1 INNER JOIN dbo.[Cost Center Mapping] t2 ON t1.[Cost Center] = t2.[Cost Center] AND t2.[Sub Product UBR Code] IN (" & selection & ")"Set Results = Cmd1.Execute()' Clear the data from the active worksheetCells.SelectCells.ClearContents' Add column headers to the sheetheaders = Results.Fields.CountFor iCol = 1 To headersCells(1, iCol).Value = Results.Fields(iCol - 1).NameNext' Copy the resultset to the active worksheetCells(2, 1).CopyFromRecordset Results' Stop running the macroMsgBox "Data Extraction Successfully Completed"Unload MeEnd Sub[/CODE]Thanks a lot for your help in advance.:)Abhay |
|
|
|
|
|