VBA code to update / create new record from Excel to Access

前端 未结 3 1467
野的像风
野的像风 2020-12-03 03:44

I have been trying to look everywhere for an answer, but my low based skills in VBA is really not helping me to figure what I am trying to code.

I have this code so

3条回答
  •  鱼传尺愫
    2020-12-03 04:38

    After writing this out I just realized that you are using VBA so my answer won't work. But you should be able to follow what's going on. Here's the idea though. And for VBA collections have a look at this:

    VBA Collections

        // First build your list
        Dim myRecords As New Collection
    
        For i = 4 To 16
        x = 0
        Do While Len(Range("E" & i).Offset(0, x).Formula) > 0
    
                    var list = from t in myRecords
                               where t.Products == Range("C" & i).Value
                               && t.Region == Range("B2").Value
                               && t.Quarter == Range("E3").Offset(0, x).Value
                               && t.Year == Range("E2").Offset(0, x).Value
                               select t;
    
                    var record = list.FirstOrDefault();
    
                    if (record == null)
                    {
                        // a record with your key doesnt exist yet.  this is a new record so add a new one to the list
                        record = new CustomObject();
                        record.Products = Range("C" & i).Value;
                        //  etc.  fill in the rest
    
                        myRecords.Add(record);
                    }
                    else
                    {
                        // we found this record base on your key, so let's update
                        record.Units += Range("E" & i).Offset(0, x).Value;                
                    }
    
        x = x + 1
        Loop
    Next i
    
                    // Now loop through your custom object list and insert into database
    

提交回复
热议问题