21. april 2004 - 15:49Der er
9 kommentarer og 1 løsning
tilføj to elementer til liste
hej, jeg vil lave en kombo-boks, hvor man kan tilføje et element hvis det ikke er på listen i forvejen. Min kode ser således ud:
Private Sub Subject_NotInList(NewData As String, Response As Integer) Dim db As DAO.Database Dim rs As DAO.Recordset Dim strMsg As String
strMsg = "'" & NewData & "' is not an available subject name " & vbCrLf & vbCrLf strMsg = strMsg & "Do you want to associate the new subject to the current list?" strMsg = strMsg & vbCrLf & vbCrLf & "Click Yes to link or No to re-type it."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then Response = acDataErrContinue Else Set db = CurrentDb Set rs = db.OpenRecordset("Subject", dbOpenDynaset) On Error Resume Next rs.AddNew rs!Subject = NewData rs.Update
If Err Then MsgBox "An error occurred. Please try again." Response = acDataErrContinue Else Response = acDataErrAdded
End If
End If rs.Close Set rs = Nothing Set db = Nothing End Sub
Mit spørgsmål er: Hvad gør jeg, hvis jeg har en kombo-boks der er baseret på en tabel med to felter, og jeg skal tilføje nye elementer? Jeg skal jo have mulighed for at tilføje to elementer på een gang? Min tabel består af en forkortelse samt det fulde navn. Så når jeg skal tilføje en ny forkortelse, skal jeg også have mulighed for at tilføje en nyt navn, der hører sammen med forkortelsen.
Dim prompt As String DoCmd.SetWarnings False Response = acDataErrContinue prompt = "Denne type findes ikke i listen, ønsker du at oprette den?" If MsgBox(prompt, vbYesNo, "Typen findes ikke!") = vbYes Then DoCmd.RunSQL "INSERT into Register (feltnavn) VALUES ('" & NewData & "')" Response = acDataErrAdded End If DoCmd.SetWarnings True
Private Sub Subject_NotInList(NewData As String, Response As Integer) Dim db As DAO.Database Dim rs As DAO.Recordset Dim strMsg As String Dim Felt2 as string
strMsg = "'" & NewData & "' is not an available subject name " & vbCrLf & vbCrLf strMsg = strMsg & "Do you want to associate the new subject to the current list?" strMsg = strMsg & vbCrLf & vbCrLf & "Click Yes to link or No to re-type it."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then Response = acDataErrContinue Else Set db = CurrentDb Set rs = db.OpenRecordset("Subject", dbOpenDynaset) On Error Resume Next Felt2 = inputbox("Angiv værdien for Felt2", "Opret ny post") rs.AddNew rs!Subject = NewData rs!Felt2 = Felt2 rs.Update
If Err Then MsgBox "An error occurred. Please try again." Response = acDataErrContinue Else Response = acDataErrAdded
End If
End If rs.Close Set rs = Nothing Set db = Nothing End Sub
Private Sub Subject_NotInList(NewData As String, Response As Integer) Dim db As DAO.Database Dim rs As DAO.Recordset Dim strMsg As String Dim Felt2 as string
strMsg = "'" & NewData & "' is not an available subject name " & vbCrLf & vbCrLf strMsg = strMsg & "Do you want to associate the new subject to the current list?" strMsg = strMsg & vbCrLf & vbCrLf & "Click Yes to link or No to re-type it."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then Response = acDataErrContinue Else Felt2 = inputbox("Angiv værdien for Felt2", "Opret ny post") If len(Felt2) = 0 then msgbox "Oprettelsen af posten afbrydes!", vbexclamation exit sub endif Set db = CurrentDb Set rs = db.OpenRecordset("Subject", dbOpenDynaset) On Error Resume Next rs.AddNew rs!Subject = NewData rs!Felt2 = Felt2 rs.Update
If Err Then MsgBox "An error occurred. Please try again." Response = acDataErrContinue Else Response = acDataErrAdded
End If
End If rs.Close Set rs = Nothing Set db = Nothing End Sub
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.