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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

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,101,759
Messages
5,482,704
Members
407,359
Latest member
aaawww

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top