Consulting

Results 1 to 2 of 2

Thread: Linked Excel file, need to add new record if word not found

  1. #1
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location

    Linked Excel file, need to add new record if word not found

    Hello

    I am linking to an excel file (Word list.xlsx), in order to see if the results of a query are within the spreadsheet.

    The table/xlsx file has just two columns:
    "sWord"
    "Senti"

    When I am checking the current value against the table, if the "Word" field doesn't have this , I am prompting the user to confirm it's value (for use elsewhere in the database).

    What I am needing, is to put the missing "sWord" and "Senti" into the original Excel file.

    I have thought about using an Access query, but when I have tried to use it, it says the file is in use (which is strange, as I can open the xslx file in explorer and edit with no issue).


    Function addNEWtoSenti(sWordval As String, SentiVal As String)
    Dim db As Database
    Dim rec As Recordset
    Set db = CurrentDb
    Set rec = db.OpenRecordset("Select * from Table")
    rec.AddNew
    rec("sWord") = sWordval
    rec("Senti") = SentiVal
    rec.Update
    Set rec = Nothing
    Set db = Nothing
    End Function

    1) Is an append query the way to go?
    2) Do I need to unlink the file and re-link it for this to work?

    Thanks all
    Last edited by OTWarrior; 02-22-2017 at 06:45 AM.
    -Once my PC stopped working, so I kicked it......Then it started working again

  2. #2
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    Although I would still like to know if the above is possible, the solution I have come up with is to not have a linked table to an XLSX file, but to have a static table.

    This way, Access is able to easily add a new record to it.

    Once this is done (for however many records there are) it will export this to the network (As both a backup and if other people copy the frontend locally).

     'NewWord set to false after Dim'ing it as Boolean, then if a new word is found, set to true
     If NewWord = True Then
         'delete spreadsheet
         Kill "Z:\Words.xlsx"
         'create a new one, based on the table
         DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Senti", "Z:\Words.xlsx"
     End If
    The kill command is to delete the existing xslx file at the location, as it doesn't seem to override if the file exists.

    Also, if the file on the network path is updated, I have a button elsewhere that will update the database with this.

    DoCmd.DeleteObject acTable, "Senti"
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Senti", "Y:\Words.xlsx", True

    Hope that helps anyone else with a similar issue
    -Once my PC stopped working, so I kicked it......Then it started working again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •