0

How do you insert multiple values into a Lookup Field in an Access database using ASP?

(I've tried a few approaches, so I'm not even sure which code to show as an attempt.)

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
user3501463
  • 55
  • 2
  • 9
  • 1
    Two things: 1) Do you know how to interact with a database with ASP? An introductory tutorial on these technologies is probably a good place to start. 2) "Insert multiple values in a field" sounds like a design problem. You *almost never* want to put *multiple* values in *one* field. Relational databases are designed to separate data into relational models. – David May 26 '14 at 19:57
  • Insert or select. The aim of the insert function will be: There are more than one Agent on the system. An Agent must search when adding a contractor to see if the contractor exists, if the contract exists the Agent can select to have the Contractor linked to their "profile" Lookup field is the AgentID – user3501463 May 26 '14 at 20:01
  • That response sounds like you copied/pasted it from a homework assignment. It also doesn't address either of the points made in my earlier comment. If you're trying to learn how to interact with a database in ASP then again I recommend finding and following an introductory tutorial on the subject. If you try something and it doesn't work in some unexpected way, we can help with specific questions about that. But we don't provide end-to-end tutorials here. Also, as a general piece of advice, storing multiple values in a single field is the wrong design. – David May 26 '14 at 20:04
  • @David It's related to [this comment](http://stackoverflow.com/questions/23874690/using-a-multi-value-lookup-field-in-a-where-clause-under-adodb#comment36753090_23875971) in an answer to his previous question. They are referring to Lookup Fields in Microsoft Access, that I built using the properties UI and/or Lookup Wizards. Sounds like they don't have much experience with relational data and understanding where a Lookup field get's it's values from. – user692942 May 26 '14 at 20:10

1 Answers1

2

For a sample table named [Agents] with a multi-value Lookup Field named [Languages] ...

TableDesign.png

the following VBScript code represents one way to add a new Agent named "Maria" who speaks both English and Spanish

Option Explicit
Dim con, cmd, rst, newID
Const adInteger = 3
Const adVarWChar = 202
Const adParamInput = 1
Const adOpenStatic = 3
Const adLockOptimistic = 3

Set con = CreateObject("ADODB.Connection")
con.Open _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=C:\Users\Public\Database1.accdb"

' insert all fields *except* multi-value Lookup Field
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO Agents (AgentName) VALUES (?)"
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "Maria")
cmd.Execute 
Set cmd = Nothing

' get AutoNumber ID of newly-inserted record
Set rst = CreateObject("ADODB.Recordset")
rst.Open "SELECT @@IDENTITY", con, adOpenStatic, adLockOptimistic
newID = rst(0).Value
rst.Close
Set rst = Nothing

' insert multi-value Lookup Field values
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO Agents (Languages.Value) VALUES (?) WHERE AgentID=?"
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255)
cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput)
cmd.Prepared = True
cmd.Parameters(1).Value = newID
' first value
cmd.Parameters(0).Value = "English"
cmd.Execute
' second value
cmd.Parameters(0).Value = "Spanish"
cmd.Execute
Set cmd = Nothing

con.Close
Set con = Nothing

While this may answer the immediate requirements of the question, it is important to note that:

  1. Access SQL support for manipulating Lookup Fields is incomplete and can be inconsistent from one development environment to another,

  2. "Microsoft strongly recommends against using Access in web applications" (ref: here), and

  3. Seasoned Access developers recommend against using Lookup Fields (ref: here) except in very specific circumstances (e.g., for integration with SharePoint).

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • +1 For the Parametrised Query approach. You could have done the `INSERT` and the `SELECT @@identity` together though to save calls to the db, although not sure if that is supported in Access. – user692942 May 27 '14 at 14:22
  • 1
    @Lankymart Thanks for the feedback. Actually, for Access the INSERT and SELECT @@IDENTITY queries do have to be executed separately. – Gord Thompson May 27 '14 at 14:29
  • 1
    Must admit I wasn't sure. Find it puzzling when people *still* use Access as a database back-end for web applications, even Microsoft don't recommend it. – user692942 May 27 '14 at 14:33