Avatar billede epimp Nybegynder
21. juli 2006 - 11:53 Der er 8 kommentarer og
1 løsning

VBA: Insert query skal slå id-reference op i anden tabel

Jeg sidder med en insert query i VBA, som skal indsætte data i en tabel, som har en relation en anden tabel. Jeg har et kundenavn, som jeg gerne vil lave om til et kundeID, således at jeg kan indsætte et entry i min salgstabel.

Hvordan gør jeg? RunSQL tager jo kun actionqueries.
Avatar billede terry Ekspert
21. juli 2006 - 12:08 #1
Where does the data come fromwhich has to be inserted into a table?



If you have two table which are related and you want to insert the data into a third table then maka e query where you select the data from th etwo tables first. Then use this query in another query to insert into the third table.


If you only have two tables then it sounds to me as though you want to update on eof the tables with data from the other table.

Maybe some more information might help
Avatar billede epimp Nybegynder
21. juli 2006 - 12:35 #2
I have three tables:
Sales: SaleID, Quantity, ItemID, CustomerID, Date
Customers: CustomerID, CustomerName
Items: ItemID, ItemName, ItemPrice

I also have a text file that I read line by line and split into the following variables:
ImportedQuantity, ImportedCustomerName, ImportedItemName, Date

Now I want to get the corresponding CustomerID and ItemID for CustomerName and ItemName, so I can do an insert into the Sales table.
Avatar billede terry Ekspert
21. juli 2006 - 13:27 #3
But the text file has nothing to do with INSERT does it?
Avatar billede terry Ekspert
21. juli 2006 - 13:33 #4
OK, I am guessing that the text file maybe has somethng to do with the insert.

What I suggest i syou imprt the text file into a temporary table then you use this in a query so that you can related the item and customer table. Then you can use this query in an append query to sales table.

You cant (easily) do this in a single query.
Avatar billede epimp Nybegynder
21. juli 2006 - 13:51 #5
Following example from http://groups.google.com/group/microsoft.public.access/browse_thread/thread/1e749029e44e6ad9/7f99792385e3d85c#7f99792385e3d85c made it work for me. Yay!


Function GetMailList()
Dim MyDB As DAO.Database, MyRec As DAO.Recordset, MyList As String
Set MyDB = CurrentDb
Set MyRec = MyDB.OpenRecordset("Select email From TableName")
While Not MyRec.EOF  ' Loop trough the table
    MyList = MyList & ";" & MyRec![email]
    MyRec.MoveNext
Wend
MyList = Mid(MyList, 2)

' use you code here with the mail list ceated

MyRec.Close
MyDB.Close
End Function
Avatar billede terry Ekspert
21. juli 2006 - 14:11 #6
I'm obviously missing something, what does the link have to do with your question?
Avatar billede epimp Nybegynder
21. juli 2006 - 14:51 #7
It made things work.
Avatar billede terry Ekspert
21. juli 2006 - 16:18 #8
Well thats whats import isnt it :o)
Avatar billede terry Ekspert
23. juli 2006 - 17:05 #9
import = important :o)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester