views:

71

answers:

1

This code is from an Access 2007 project I've been struggling with. The actual mean part is the part where I should put something like "update only current form"

DoCmd.RunSQL "Update Korut Set [PikkuKuva]=('" & varFile & "') ;"

Could someone please help me with this?` If I use it now, it updates all the tables with the same file picked.

Heres the whole code.

  ' This requires a reference to the Microsoft Office 11.0 Object Library.

  Dim fDialog As Office.FileDialog
   Dim varFile As Variant
   Dim filePath As String


   ' Set up the File dialog box.
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   With fDialog
      ' Allow the user to make multiple selections in the dialog box.
      .AllowMultiSelect = False

      ' Set the title of the dialog box.
      .Title = "Valitse Tiedosto"

      ' Clear out the current filters, and then add your own.
      .Filters.Clear
      .Filters.Add "All Files", "*.*"

      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
      If .Show = True Then
         ' Loop through each file that is selected and then add it to the list box.
         For Each varFile In .SelectedItems
            DoCmd.SetWarnings True
            DoCmd.RunSQL "Update Korut Set [PikkuKuva]=('" & varFile & "') ;"
         Next
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
+2  A: 

This is something of guess work as you do not say where you are running the code, but as a general rule, you need something on the lines of:

 DoCmd.RunSQL "Update Korut Set [PikkuKuva]=('" & varFile & "') WHERE ID=" 
               & Me.ANumericID

If the unique value for your form is text, you will need quotation marks:

 DoCmd.RunSQL "Update Korut Set [PikkuKuva]=('" & varFile & "') WHERE ID='" 
               & Me.ATextIDWithNoSingleQuotes & "'"
Remou
To be more explicity about what @Remou is saying: you have no WHERE clause on your UPDATE statement, so it will update all rows. You need a WHERE clause that limits each update to the single row or group of rows that you want to store the filename in.
David-W-Fenton
Thanks to both, got it working to check from another table.Now I'm struggling with how to copy the Autonumber ID to secondary field. I'm currently trying to "db.OpenRecordset" on form_load, but I'm misleaded by how to copy x->y.
BashLover
Post another question, it will be picked up quite quickly.
Remou