Page 1 of 3 123 LastLast
Results 1 to 15 of 45
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365

    Add Blank Row After Group 2

    Hi Guy's, any advice on this problem please ? Firstly thank you to WGM, June7, Micron and any others who had an input into my previous post (All Blank Row After Group)

    So based on my previous post, all is working perfect, see after code an issue i have never come accross

    Code:
    Const NumRows As Integer = 1 ' Number Of Blank RowsConst TargetClm As String = "C"
    Const TargetClm2 As String = "D" ' change to suit
    Const TargetClm3 As String = "E" ' change to suit
    Const TargetClm4 As String = "F" ' change to suit
    Const TargetClm5 As String = "G" ' change to suit
    Const TargetClm6 As String = "H" ' change to suit
    Const TargetClm7 As String = "I" ' change to suit
    
    
    Dim rs As DAO.Recordset
    Dim xlAP As Object, xlWB As Object
    Dim strPath As String, strFile As String, sSQL1 As String
    Dim xlSHT1 As EXCEL.Worksheet, xlSHT2 As EXCEL.Worksheet, xlSHT3 As EXCEL.Worksheet, xlSHT4 As EXCEL.Worksheet, xlSHT5 As EXCEL.Worksheet, xlSHT6 As EXCEL.Worksheet
    Dim xlSHT7 As EXCEL.Worksheet, xlSHT8 As EXCEL.Worksheet, xlSHT9 As EXCEL.Worksheet, xlSHT10 As EXCEL.Worksheet, xlSHT11 As EXCEL.Worksheet, xlAllSHTS As EXCEL.Worksheets
    Dim iWHQty As Integer, iAllocated As Integer, iAvailable As Integer, intCount As Integer, intID As Integer, i As Integer, x As Integer, intLR As Integer
    Dim r As Long ' Loop Counter
    
    
        strPath = "C:\Users\davem\Desktop\"
        strFile = "My File Name.xlsx"
        
        Set xlAP = CreateObject("Excel.Application")
        Set xlWB = xlAP.Workbooks.Open(strPath & strFile)
        
        xlAP.Visible = True
        
        Set xlSHT1 = xlWB.Worksheets(1)
    '############### NOTE SORT NO IS SET AS NUMBER
    
    
        sSQL1 = "SELECT tblStock.SortNo, tblStock.PONumber, tblStock.ItemType, tblStock.LiftNo, tblStock.StartQty, tblStock.AllocatedQty, tblStock.NewQty" _
                        & " FROM tblStock" _
                        & " WHERE (((tblStock.SortNo) < 99) And ((tblStock.ItemNo) Is Not Null))" _
                        & " ORDER BY tblStock.SortNo;"
    
    
        Set rs = CurrentDb.OpenRecordset(sSQL1)
        
        With xlSHT1
        
        intCount = rs.RecordCount
        
        
            xlSHT1.Cells(3, 3).CopyFromRecordset rs
        
            
            For r = (.Cells(.Rows.Count, TargetClm).End(xlUp).Row - 1) To 2 Step -1
                If .Cells(r, TargetClm).Value <> .Cells(r + 1, TargetClm).Value Then
                    .Cells(r + 1, TargetClm).Resize(NumRows).EntireRow.Insert
                    .Cells(r + 1, TargetClm).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm2).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm3).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm4).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm5).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm6).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm7).Interior.ColorIndex = 15
                End If
            Next r
        
        
      '      intID = 1
      '      Do While x < intCount
      '          x = x + 1
      '          If intID <> .Cells(3 + x, 1).Value Then
      '              .Cells(3 + x, 1).EntireRow.Insert
      '              intID = intID + 1
      '              intCount = intCount + 1
      '          x = x + 1
      '          End If
      '      Loop
      
      
            intLR = xlSHT1.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                
                .Range("J3:J" & intLR + 2).Interior.ColorIndex = 15
                .Cells.EntireColumn.HorizontalAlignment = xlLeft
                .Cells(intLR + 2, 3) = "TOTALS:"
                .Cells(intLR + 2, 3).Interior.ColorIndex = 15
            iWHQty = DSum("StartQty", "tblStock", "[LiftType] Is Not Null")
                .Cells(intLR + 2, 7) = iWHQty
                .Cells(intLR + 2, 7).Interior.ColorIndex = 15
            iAllocated = DSum("AllocatedQty", "tblStock", "[LiftType] Is Not Null")
                .Cells(intLR + 2, 8) = iAllocated
                .Cells(intLR + 2, 8).Interior.ColorIndex = 15
            iAvailable = DSum("NewQty", "tblStock", "[LiftType] Is Not Null")
                .Cells(intLR + 2, 9) = iAvailable
                .Cells(intLR + 2, 9).Interior.ColorIndex = 15
            
            
         
        End With
        
        
        
        Set xlAP = Nothing
        Set xlWB = Nothing
        Set xlSHT1 = Nothing
    I am using a different database at home than work, i upload to work when i want to copy vb code

    When i run the update at work the SortNo field outputs to date (field set as number)

    so i have tried a few things,


    1 formatting the colomn in Excel (column C) to Either General or Number and saving
    2 created a brand new excel template to eliminate excel being the issue
    3 adding .Range("C3:C" & intLR).NumberFormat = "0"
    4 Done Compact And Repair

    so update at work in that particular column is outputting as date 01/01/1900, my instant fix is when the file opens, re format but, the next part is bugging me
    when i try the same code, at home on my laptop, always outpus correct as number

    so mt next plan is create a brand new database, and imnport all objects for existing to try db file

    before i do this, any suggestions why the same update will out put different ??

    KIndest and thank you as always

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I would run a query with that recodset sql and see what you actualy start with on each computer.
    Break it down into small steps and test each in turn.

    Or if you cannot be bothered to do that. copy your home db over your work db, providing the data is the same?

    Myself, I would be doing my first suggestion, as it would intrigue me as to why.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    Suspect it is an excel issue rather than access . I use a routine similar to the one to.format rows - loop through the recordset fields collection formatting the equivalent column depending on datatype. Sometimes you need a meta table to differentiate between different types of numbers- quantity, price, value for example or you want additional formatting such as font, backcolor or borders. Doesn’t have to be a physical table, could be something stored in the calling form or just hardcoded in your vba

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Thank you guy's @WGM, what points i should have added to my testing

    i tried running records from SQL then pulled to a query, query has got numbers 1 to 15 in the sort no field (clicking and opening query)

    so when riunning query, all is correct, when i change Set rs (SQL) to Set rs ("qry), dates are still outputting

    db at home is just a testing db so i can't replace the work one

    will check a few more things as you guys have mentioned, thanks CJ

    another test i am thinking of, is copy the table to table2 (backing up purpose) then try with having 1 record at a time

    i guess start from stored data to setting references to stepping through records...

    Thanks you both and anyone else who suggests

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Work DB

    Query

    Click image for larger version. 

Name:	Query Snip.JPG 
Views:	21 
Size:	10.2 KB 
ID:	52557

    VBA

    Code:
    sSQL = "SELECT tblStock.SortNo, tblStock.PONumber, tblStock.ItemType, tblStock.ItemNo, tblStock.StartQty, tblStock.AllocatedQty, tblStock.NewQty" _                    
    & " FROM tblStock" _
                        & " WHERE (((tblStock.SortNo) < 99) And ((tblStock.ItemNo) Is Not Null))" _
                        & " ORDER BY tblStock.SortNo;"
    
    
    'Tried setting rs (SQL)
    
    
    'Then setting direct to query
    
    
            Set rs = CurrentDb.OpenRecordset("qryStockUpdate")
    Excel Result Work

    Click image for larger version. 

Name:	Excel Work.JPG 
Views:	21 
Size:	16.6 KB 
ID:	52558

    Excel Result Home

    Click image for larger version. 

Name:	Excel Result Home.JPG 
Views:	21 
Size:	9.6 KB 
ID:	52559

    will check out what what you guys have suggested! find it bizarre becasue code is the same on Work db (work) and Test db (laptop home)

    thanks again

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Just throught what I could try, is create all headers on my home Excel file, test it from home db, upload the home excel file to work!!

    lets see what i can find and take it form there

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Try a new Excel workbook, as I believe that is to blame.
    Or format that column as number, integer, save and start again. Somehow you have changed that to a date format.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    @WGM, yes something is amiss, going to come back to it later but just downloaded excel file form work and run it from home db and does out put as number correctly so somewhere in the procedure number is being changed to date, i am wondering if i can force that field in the SQL statement to be integer, perhaps i try renaming the field name in the query such as SortGroup AS [SortNo] and paste the SQL in for recordset to read ?

    i am gussing that will eliminate the db field even though it's set as number and numbers are stored

    appreciate your inout though

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Well I reckon your Excel workbook is at fault and you appear to have proved that by using the work workbook?

    However if you create a query qdf, in that you can specify a format.

    Here is my ID, which as you know is Long integer, but formatted as currency.
    So you could use a querydef instead of sql string?
    Attached Thumbnails Attached Thumbnails DMT ID as Currency.png  
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Hi Guy's i have found when this happens, if i run x 1 recordset update, sheet 1 Column C remains number, when the procedure follows to next update (didn't have this on home laptop), downloaded from work and added sheet2 update SQL

    tried a numebr of times to see if it happend every time and yes it does, if i exit sub after sheet 1 update, it's fine, remove exit sub and let sheet 2 update (sheet 1 Column C) becomes a date so that tells me it's access not Excel ?

    I will remove sensitive data and paste vba over if not todat, definite tomorrow, thank you all indeed

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Not really?
    Show your code.
    Anyway, it is not Access or Excel at fault, but you, as it works in one system and not the other.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Yeh think you are correct there WGM, will remove senstive data and paste on

  13. #13
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Here we go, i am thinking is shipment date in sheet 2 update ? i am not sure, any advice now knowing where this is happening ?

    Code snip (Note Exit Sub Before Sheet 2 update), leave exit sub in and sheet 1 is correct

    remove exit sub to resume, sheet 1 now is date

    ??????????

    Code:
        Set xlAP = CreateObject("Excel.Application")
        Set xlWB = xlAP.Workbooks.Open(strPath & strFile)
       
        xlAP.Visible = True
       
        Set xlSHT = xlWB.Worksheets(1)
     
        sSQL = "SELECT tblStock.SortNo, tblStock.PONumber, tblStock.ItemType, tblStock.ItemNo, tblStock.StartQty, tblStock.AllocatedQty, tblStock.NewQty" _
                        & " FROM tblStock" _
                        & " WHERE (((tblStock.SortNo) < 99) And ((tblStock.ItemNo) Is Not Null))" _
                        & " ORDER BY tblStock.SortNo;"
     
        Set rs = CurrentDb.OpenRecordset(sSQL)
       
        With xlSHT
       
        intCount = rs.RecordCount
       
        .Cells(3, 3).CopyFromRecordset rs
           
            For r = (.Cells(.Rows.Count, TargetClm).End(xlUp).Row - 1) To 2 Step -1
                If .Cells(r, TargetClm).Value <> .Cells(r + 1, TargetClm).Value Then
                    .Cells(r + 1, TargetClm).Resize(NumRows).EntireRow.Insert
                    .Cells(r + 1, TargetClm).RowHeight = "09.00"
                    .Cells(r + 1, TargetClm).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm2).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm3).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm4).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm5).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm6).Interior.ColorIndex = 15
                    .Cells(r + 1, TargetClm7).Interior.ColorIndex = 15
                   
                End If
            Next r
       
            intLR = xlSHT.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Range("B3:B" & intLR + 2).Interior.ColorIndex = 15
                .Range("J3:J" & intLR + 2).Interior.ColorIndex = 15
                .Cells.EntireColumn.HorizontalAlignment = xlLeft
                .Cells(intLR + 2, 3) = "TOTALS:"
                .Cells(intLR + 2, 3).Interior.ColorIndex = 15
            iWHQty = DSum("StartQty", "tblStock", "[ItemType] Is Not Null")
                .Cells(intLR + 2, 7) = iWHQty
                .Cells(intLR + 2, 7).Interior.ColorIndex = 15
            iAllocated = DSum("AllocatedQty", "tblStock", "[ItemType] Is Not Null")
                .Cells(intLR + 2, 8) = iAllocated
                .Cells(intLR + 2, 8).Interior.ColorIndex = 15
            iAvailable = DSum("NewQty", "tblStock", "[ItemType] Is Not Null")
                .Cells(intLR + 2, 9) = iAvailable
                .Cells(intLR + 2, 9).Interior.ColorIndex = 15
           
        End With
       
        Set rs = Nothing
        Set xlSHT = Nothing
     
    Exit Sub
     
    'SHT2
       
        Set xlSHT = xlWB.Worksheets(2)
       
        sSQL = "SELECT tblEdit.ShipmentDate, tblEdit.DelTo, tblEdit.Town, tblEdit.SNumber, tblEdit.PONumber, tblEdit.ItemType, tblEdit.ItemNo, tblEdit.Status " _
                        & "FROM tblEdit " _
                        & "WHERE Customer = '" & strCust & "'" & " And Status = '" & myStatus & "'" & " Or Status = '" & myStatus2 & "'" & " Or Status = '" & myStatus3 & "'" & " And " _
                        & "ShipmentDate Is Null" & " And Source = '" & strSRC & "' ORDER BY DelTo, ShipmentDate DESC"
                    Set rs = CurrentDb.OpenRecordset(sSQL)
           
        Set rs = CurrentDb.OpenRecordset(sSQL)
           
            With xlSHT
           
            .Cells(3, 3).CopyFromRecordset rs
           
            intLR = xlSHT.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Range("B3:B" & intLR + 2).Interior.ColorIndex = 15
                .Range("K3:K" & intLR + 2).Interior.ColorIndex = 15
                .Cells.EntireColumn.HorizontalAlignment = xlLeft
           
            End With
           
        Set rs = Nothing
        Set xlSHT = Nothing
    Stop sheet 2 update

    Click image for larger version. 

Name:	Excel Result SHeet1.JPG 
Views:	16 
Size:	11.5 KB 
ID:	52561

    Remove exit and resume sheet 2 update

    Click image for larger version. 

Name:	Excel Result Both.JPG 
Views:	16 
Size:	26.1 KB 
ID:	52562

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    What is your first field in the 2nd sql ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Date field which is supposed to be null, the target is to show records that have no shuipment date yet

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Add Blank Row After Group
    By DMT Dave in forum Access
    Replies: 11
    Last Post: 01-10-2025, 03:13 AM
  2. Replies: 3
    Last Post: 11-18-2016, 12:03 PM
  3. Replies: 4
    Last Post: 08-31-2016, 12:22 AM
  4. Replies: 5
    Last Post: 04-29-2014, 06:42 PM
  5. Replies: 1
    Last Post: 11-28-2013, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums