Avatar billede poulerikdahl Nybegynder
08. oktober 2003 - 11:48 Der er 5 kommentarer og
1 løsning

SELECT en variabel ud til at teste på

Kan nogen hjælpe en absolut nybegynder i Access her. ?
Jeg har en formular i Access med en bagvedliggende kode. Jeg vil gerne indsætte en record i en tabel og det går fint. Dog vil jeg inden lige checke om recorden allerede findes, og vil således selecte for at checke.
Min SQL2 er insertkoden, og SQL5 er min check-select.
Min nybegynderkode i uddrag ser nogenlunde således ud :

Dim dummy As String
dummy = "0"
SQL5 = "Select 1 into '&dummy&' from el where telefon = " & f2 & ""
DoCmd.RunSQL SQL5
    If dummy = "0" Then
      DoCmd.RunSQL SQL2
    End If

....men dummy bliver ved med at være 0, og bliver aldrig 1.

Er godt klar over at det sikkert er noget med mine "plinger" "&" osv. der ikke er syntax-korrekt; men har nogen et bud. Hvordan skriver man variabel-navnet dummy ind. Tror at Access tror at det blot er en tekststreng?

mvh
Poul-Erik
Avatar billede terry Ekspert
08. oktober 2003 - 12:09 #1
Not sure what your problem is!
You say "....og det går fint"
then you say
"....men dummy bliver ved med at være 0, og bliver aldrig 1."

So it doesnt go well (fint)!!


You can use

If Dcount("*", "YourTable", "telefon = " & f2) = 0 Then ...


Is this the name of the table? if it is then I suggest you change it.
You should keep to letters and numbers and NOT Danish characters

'&dummy&'
Avatar billede terry Ekspert
08. oktober 2003 - 12:12 #2
Now I'm starting to wake up!
IF you are updating (changing) an existing reocrd use
UPDATE Table SET field = ...

If you are creating a new record then use

INSERT INTO (fieldList) VALUES (valuesList)

So what is it you are trying to do?
Avatar billede poulerikdahl Nybegynder
08. oktober 2003 - 12:32 #3
Hi Terry

No, I only need to create the record if it doesn't exists, and then later have another formular to popup with the record. (that's another scope, and it works)
I'll try the Dcount, and will be back
Thanks
Poul-Erik
Avatar billede poulerikdahl Nybegynder
08. oktober 2003 - 12:41 #4
Hi again Terry

Wauw, it works.
Here's the total code :

Private Sub Spm8_2_DblClick(Cancel As Integer)

  Dim SQL2 As String
  Dim SQL3 As String
  Dim SQL4 As String
  Dim f1 As String
  Dim f2 As String
  Dim f3 As String
  Dim f4 As String
  Dim dummy As String
 
    dummy = "0"
    f2 = Form_Relevantekunder.telefon
    f1 = Form_Relevantekunder.bpnr
    f3 = Format(Date, "yyyymmdd")
    f4 = "El"
    SQL3 = "Values(" & "'" & f1 & "'" & "," & "'" & f2 & "'" & "," & "'" & f3 & "'" & ")"
    SQL2 = "INSERT INTO el(bpnr, telefon, dato)" & SQL3
    SQL4 = "Telefon = " & f2 & ""
    If DCount("*", "El", "telefon = " & f2) = 0 Then
      DoCmd.RunSQL SQL2
    End If
    DoCmd.OpenForm "El", , , SQL4
End Sub

...and I only needed to check for existence before calling the new formular, and didn't want to create the record again.
I will need more help later for sure, and will be back. I seem to have a problem to get the first formular reselected on return from the second.
This is my first Access-application,.....and my first question in this forum, and I have to see how I can pay you the points
Avatar billede terry Ekspert
08. oktober 2003 - 18:55 #5
Great to hear it works!
I think I see the problem with opening the form "el"

SQL4 = "Telefon = '" & f2 & "'"
DoCmd.OpenForm "El", , , SQL4

If telephone i stext then it has to be inside '' if its a number then not!
I can see in your insert that f2 is inside '' so I am assumimg that it is text.

mvh
Terry
Avatar billede poulerikdahl Nybegynder
09. oktober 2003 - 09:40 #6
I see your point Terry.....
But it IS a number-field, and it works.....don't know why. (It shouldn't..)
My other problem with reselection on return is solved too, by using
Requery
...and going to currentRecord
DoCmd.GoToRecord acDataForm, "el", acGoTo, currrec

This Forum helps a lot. Just searching the questions for a problem, and others have had the same problem, and got the help needed.....often from you Terry, thanks !!
My greatest problem is to be familiar with the syntax here in Access. I'm used to ORACLE-SQL, and there is a difference for sure

Poul-Erik
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