when i run my excel vba code it creates duplicate columns

ubaig

New Member
Joined
Mar 18, 2014
Messages
17
whenever i run my excel2007vba code download code it will create an duplicate copies of multiple column in the sheet please let me know the solution

Option Explicit
'Public strConn As String
Public projCode, batchCode, batchID, prdTblName
Public Const strConn = "Provider=MSDASQL.1;Password=bmx1;Persist Security Info=True;User ID=root;Data Source=mysql32;Initial Catalog=wds"
Sub init()
projCode = ""
batchCode = ""
UserForm1.Show


End Sub




Sub dbConnect()
Dim strConn As String
Dim dbconn As New ADODB.Connection

strConn = "Provider={MySQL ODBC 5.2 ANSI Driver};Password=bmx1;Persist Security Info=True;User ID=root;Data Source=mysql32;Initial Catalog=wds"
' strConn = "DSN=mysql32;DESCRIPTION=data;SERVER=localhost;UID=root;PWD=bmx1;DATABASE=wds;PORT=3306"
strConn = "Provider=MSDASQL.1;Password=bmx1;Persist Security Info=True;User ID=root;Data Source=mysql32;Initial Catalog=wds"
'dbconn.Open dsn = mySQL_UserDSN
dbconn.Open strConn


If dbconn.State = adStateOpen Then
MsgBox "Connected"
Else
MsgBox "DB Connection Failed"
End If
dbconn.Close

End Sub

Sub updateData()
Dim dbconn As New ADODB.Connection


Dim totColumns, totRows, i, j, WDS_id
Dim prBatchName, prTableQry, inTableQry, prTableQry1, dbQry
Dim rs As New ADODB.Recordset


Sheet1.Activate
prBatchName = "tblProd_AGR_007"


totColumns = ActiveSheet.Cells(2, 1).CurrentRegion.Columns.Count
totRows = ActiveSheet.Cells(2, 1).CurrentRegion.Rows.Count


dbconn.Open strConn
rs.Open "select * from " & prBatchName, dbconn, adOpenStatic, adLockOptimistic
For j = 3 To totRows
WDS_id = ActiveSheet.Cells(j, 1)
rs.Find "WDS_ID=" & WDS_id

For i = 2 To totColumns
rs(ActiveSheet.Cells(2, i).ID) = ActiveSheet.Cells(j, i)
Next
rs.Update

Next
rs.Close
dbconn.Close
MsgBox "Data updated sucessfully"


End Sub




Sub downloadData()

Dim dbconn As New ADODB.Connection
Dim i ', prdTblName
'prdTblName = "tblProd_" & projCode & "_" & batchCode
Dim qryStr, dataStr As String


qryStr = "SELECT * FROM tblbatch_headers where idBatch " & batchID & "order by col_seq asc"
Dim rs As New ADODB.Recordset
dbconn.Open strConn
rs.Open qryStr, dbconn, 3, 1
i = 0
Do While rs.EOF = False


i = i + 1
If i = 1 Then
dataStr = rs("tblColName")
Else
dataStr = dataStr & ", " & rs("tblColName")
End If
rs.MoveNext
Loop
' FP = For Prodction, IQR = In Process Quality Rejected, PC = Production Complete
' IQA = In Quality Check Approved, FQA = Final Quality Review Approved, FQR = Final Qaulity Review Rejected


qryStr = "SELECT " & dataStr & " FROM " & prdTblName & " where FQR_User_Code='" & Application.UserName & _
"' and line_status in('QP') order by line_status"
Sheet1.Activate
Sheet1.Columns.Clear
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=mysql32;" _
, Destination:=Range("$A$2")).QueryTable
.CommandText = qryStr
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
' .SourceConnectionFile = _
"C:\Users\Documents\My Data Sources\mydbconnection.odc"
' .ListObject.DisplayName = "Table_wds"
' .Refresh BackgroundQuery:=False
End With
Range("C2").Select


rs.MoveFirst
i = 0
Do While rs.EOF = False
Dim rng As Range
i = i + 1
ActiveSheet.Cells(1, i) = rs("Comments")
ActiveSheet.Cells(2, i) = rs("ActualColName")
ActiveSheet.Cells(2, i).ID = rs("tblColName")
rs.MoveNext
Loop
ActiveSheet.Cells(2, i + 1).ID = prdTblName
rs.Close
dbconn.Close


MsgBox "Data downloaded sucessfully"
Unload UserForm1


End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top