Convert to Long Integer in Loop

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,123
Office Version
2016
Hi guys,

as I am importing an Excel Worksheet into my access database I need some assistance to convert a column in Excel to a longInteger Field in Access.

Code:
Sub FillAllCellsWithLongInteger()
    Dim Zeile As Integer
    Dim ZeileMax As Long
    
Application.ScreenUpdating = False
    
    With ActiveSheet
        ZeileMax = .Cells(Rows.Count, 1).End(xlUp).Row
    
        For Zeile = 2 To ZeileMax
            .Cells(Zeile, 17).Value = CLng(59)
        Next
    End With
    
Application.ScreenUpdating = True
End Sub
This is what I tried.

In Excel the column(17) is blank and I fill it with the number 59. This is a dummy so to say .. As I need this column to be a field with longInteger datatype in Access I did try to convert it to Long in my code.
When I import that worksheet in access I get doule Datatyp tough. So I was wondering if someone knows how to be able to get around this.

Many thanks

Albert
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
It will be more interesting to see the code that does the import, if it is done by code that is.
Do you run the import code from access or from excel.
If you do it manually you can easily change the data format /Field size manually.
If you import in an existing table you can set the field size manually in advance.

Turns out DAO does not allow changing an existing field size you can use SQL:
Code:
CurrentDB.Execute "ALTER TABLE [COLOR=#ff0000]tblMytable [/COLOR]ALTER COLUMN [COLOR=#ff0000]fldMyField[/COLOR] LONG"
change the red ones to match you case.
 

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,123
Office Version
2016
Hi thanks Bosan42,

I do the import with access and yes via code.. it's just a TransferSpreadsheet Code

Code:
Sub ImportExcelSpreadsheet(FileName As String, TableName As String)
On Error GoTo BadFormat
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, TableName, FileName, True
    Exit Sub
BadFormat:
    MsgBox "Das war keine Excel Datei!"
End Sub
But I will try it with your SQL Statement .

Many Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,102,731
Messages
5,488,543
Members
407,645
Latest member
suyoggore

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top