Import from Access to Excel

ravaz

Board Regular
Joined
Mar 25, 2008
Messages
196
Hi

Am importing from SQL to Excel. The Data Type of the field in access is text though it contains numeric Stock Barcodes.

Many of the codes start with a zero but when I pull this into Excel the preceeding zero does not appear. The code I am using is below:

Code:
Sub Stock()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

Server_Name = "" ' Enter your server name here
Database_Name = "" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT cast(IW.StockCode as bigint),IW.StockCode,  cast(IM.ProductClass as int), sum(IW.QtyOnHand) " & _
"FROM InvWarehouse as IW, InvMaster as IM " & _
"where IW.StockCode = IM.StockCode " & _
"Group By cast(IM.ProductClass as int),cast(IW.StockCode as bigint) , IW.StockCode  " & _
"Order By cast(IM.ProductClass as int),cast(IW.StockCode as bigint),IW.StockCode "


Set Cn = New ADODB.Connection
Cn.CommandTimeout = 300
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"

rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("Stk").Range("a2") ' Enter your sheet name and range here
    .ClearContents
    .CopyFromRecordset rs
End With
'            Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi

Am importing from SQL to Excel. The Data Type of the field in access is text though it contains numeric Stock Barcodes.

Many of the codes start with a zero but when I pull this into Excel the preceeding zero does not appear. The code I am using is below:

Code:
Sub Stock()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

Server_Name = "" ' Enter your server name here
Database_Name = "" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT cast(IW.StockCode as bigint),IW.StockCode,  cast(IM.ProductClass as int), sum(IW.QtyOnHand) " & _
"FROM InvWarehouse as IW, InvMaster as IM " & _
"where IW.StockCode = IM.StockCode " & _
"Group By cast(IM.ProductClass as int),cast(IW.StockCode as bigint) , IW.StockCode  " & _
"Order By cast(IM.ProductClass as int),cast(IW.StockCode as bigint),IW.StockCode "


Set Cn = New ADODB.Connection
Cn.CommandTimeout = 300
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"

rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("Stk").Range("a2") ' Enter your sheet name and range here
    .ClearContents
    .CopyFromRecordset rs
End With
'            Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing

End Sub
Excel is dropping the leading zero because it's storing it as a number. If you precede each number with a single quote ' it will convert the number to text and not drop the zero. I'm sure there's a code method but this is all I could come up with this morning.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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