Results 1 to 3 of 3
  1. #1
    sandordan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2018
    Posts
    6

    add values a recordset with vba


    Hallo Everyone,

    I am not able to add values (imported file names "fn" to the column "file_name") pls help me

    Code:
    Public Function selectFile()
    
    DoCmd.SetWarnings False
    
    
    Dim crrs As DAO.Database
    Dim cr As DAO.Recordset
    Set crrs = CurrentDb
    
    
    On Error GoTo go1
    DoCmd.DeleteObject acTable, "qry_import"
    
    
    go1:
    On Error GoTo beenden
    Dim fdialog As FileDialog
    Dim sFolder As String
    Set fdialog = Application.FileDialog(msoFileDialogFilePicker)
    
    
    Dim pth As Variant
    
    
    
    
    With fdialog
    .InitialFileName = "S:\Automotive\CO\DE\AKMP\22_Procurement\04 Kennzahlen\03_Werkzeugmanagment\Archiv\"
    If .Show = -1 Then
    sFolder = .SelectedItems(1)
    fn = Dir(sFolder)
    End If
    End With
    
    
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "qry_import", sFolder, True, "A:N"
    Set fldnew = CurrentDb.TableDefs("qry_import").CreateField("file_name", dbLong)
    CurrentDb.TableDefs("qry_import").Fields.Append fldnew
    Set cr = crrs.OpenRecordset("qry_import")
    
    
    cr.Fields.Refresh
    
    
    cr.AddNew
    cr!file_name = fn
    
    
    DoCmd.OpenQuery "WZDB_anfügen"
    
    
    beenden:
    DoCmd.SetWarnings True
    End Function
    Thank you for your Help!

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    Couple of points - You aren't declaring all your variables, add option explicit at the top of all your modules.

    You are setting fldnew to the code to add the table field, but you don't appear to be actually creating it.
    As far as I can tell the field isn't there to add the data to. What error do you get and does this compile?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    You invoke the AddNew method, but you don't UPDATE the recordset. I think you should read up on this type of procedure (you probably just grabbed some code without really knowing the ins and outs of it). There are things that you might do, or not do, that you shouldn't (or should) in order to make this work. If after reviewing the below link you still have issues, then post back, but do fix your code as noted. Turn on Option Explicit by default. Someone once wrote that if you don't have that at the top of every module, you deserve all the grief you're going to get. I'm not a fan of deleting a table on a regular basis only to recreate it with different data. I think the general feeling out there is that it's an activity that promotes db bloat and corruption. Better to update the table or append to it, even if you have to delete all the records in it first.

    https://msdn.microsoft.com/en-us/lib...or=-2147217396
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Load values from a text box into a recordset
    By CanuckBuck in forum Programming
    Replies: 15
    Last Post: 01-19-2016, 08:56 AM
  2. Recordset edit values in fields of each record
    By Ruegen in forum Programming
    Replies: 3
    Last Post: 03-17-2014, 04:36 PM
  3. Can not locate values in recordset
    By lawdy in forum Programming
    Replies: 8
    Last Post: 02-01-2013, 11:18 PM
  4. Recordset loop & change values (kinda...)
    By opopanax666 in forum Programming
    Replies: 4
    Last Post: 05-03-2012, 04:26 AM
  5. Getting Values out of a recordset
    By theracer06 in forum Programming
    Replies: 6
    Last Post: 09-24-2010, 03:44 PM

Tags for this Thread

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