Avatar billede klk Nybegynder
21. december 2007 - 09:55 Der er 19 kommentarer og
1 løsning

Simpelt query. Hvordan skriver man koden?

Jeg prøver at lave et simpelt eksempel, hvor jeg udtrækker et felt fra den første record i et select statement, og viser resultat i messagebox. Jeg har søgt i hjælpen og på flere fora og fundet lidt syntax hist og pist, men det virker ikke. Jeg forsøger blot at lære hvorledes man trækker resultatet over i et recordset (cursor) og trækker recordset data over i en variabel, som jeg senere skal bruge. Har prøvet:

Private Function FindEmailAddresses()

'Rem    Dim db As DAO.Database
'Rem    Dim EmailRecSet As DAO.Recordset
'Rem    Dim strSQL As String

    Dim db As Database
    Dim EmailRecSet As Recordset
    Dim strSQL As String


Set db = CurrentDb()

strSQL = "Select Email1 From Tbl_Advisering Where Advi_Id = 103"
Set EmailRecSet = db.OpenRecordset(strSQL)

'Rem  EmailRecSet.OpenRecordset (strSQL)
'Rem  EmailRecSet.Open(strSQL)

Do Until EmailRecSet.EOF
  MsgBox EmailRecSet.Fields.Item(0).Value
  EmailRecSet.MoveNext
Loop

Set EmailRecSet = Nothing
EmailRecSet.Close
db.Close

End Function

Undlad venligst ! og & her i starten. Det vil jeg arbejde videre med bagefter.

Jeg skal senere erstatte 103 med et felt fra en form (Advi_Id). Det bliver vist noget med:
strSQL = "Select MAWB From Tbl_Advisering Where Advi_Id = '" & Me.Advi.Id & "' "
Når jeg kommer så vidt
Avatar billede klk Nybegynder
21. december 2007 - 10:08 #1
Jeg får en runtime error:
"Der er for få parametre. Der var ventet 1".
I linien:
Set EmailRecSet = db.OpenRecordset(strSQL)

Jeg har prøvet med:
Set EmailRecSet = db.OpenRecordset(strSQL, dbOpenSnapshot)
men det giver det samme
Avatar billede terry Ekspert
21. december 2007 - 14:55 #2
Try this, if you still get an error what data type is Advi_Id ?

Dim db As Database
Dim EmailRecSet As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

strSQL = "Select Email1 From Tbl_Advisering Where Advi_Id = 103"
Set EmailRecSet = db.OpenRecordset(strSQL)

Do Until EmailRecSet.EOF
  MsgBox EmailRecSet.Fields.Item(0).Value
  EmailRecSet.MoveNext
Loop

'Close before setting to nothing or you get an error (cant close nothing
EmailRecSet.Close
Set EmailRecSet = Nothing

db.Close

End Function
Avatar billede klk Nybegynder
21. december 2007 - 19:36 #3
I have used cut and paste to implement your code into the module. The same error comes up again. Advi_Id is of type autonummerering.

I checked the table name and field names very carefully again. They should also be correct.

I'm using Access2003
Avatar billede klk Nybegynder
21. december 2007 - 22:16 #4
Jeg tror det er mit query der er noget galt med. En lignende sag peger i den retning.
http://netcoders.dk/forum.asp?mode=show_message&tech_id=1&forum_id=3&message_id=73299
Jeg kan dog stadig ikke se nogen fejl i mit query
Avatar billede klk Nybegynder
21. december 2007 - 22:27 #5
I found an error - but it does not solve the problem.

Advi_Id is named Advi-Id in the table. Sorry.

Now the error is:
"Der er for få parametre. Der var ventet 2".
Avatar billede terry Ekspert
22. december 2007 - 17:20 #6
Any chance of seeing your dB?

ekspertenATsanthell.dk
AT = @
Avatar billede klk Nybegynder
22. december 2007 - 22:57 #7
I have send a sample database
Avatar billede terry Ekspert
23. december 2007 - 10:33 #8
strSQL = "Select Info1 From Tbl_Advisering Where [Advi-Id] = 118"


I'm off out for the day, will look again later
Avatar billede klk Nybegynder
23. december 2007 - 12:04 #9
That works! Also when I write:
Select Info1 From Tbl_Advisering Where [Advi-Id] = " & Me.Advi_Id

I can also show the Info1 value in a Msgbox

Now I am preparing what I'm really up to. I want to put the Email address from the Tbl_Kunde in a Global variable and use it in the DoCmd.SendObject command in the calling Sub.

Now the error comes up again. The column has once more the '-' or the '_' character and now the value is taken from a Combobox (Kunde_Id) than in real life shows the company name.

Code is like this at the moment

Private Function FindEmailAddresses()

Dim db As Database
Dim EmailRecSet As DAO.Recordset
Dim strSQL As String
Dim CurEmail1 As String

Set db = CurrentDb()

strSQL = "Select E-mail1 From Tbl_Kunde Where [Kunde-Id] = " & Me.Kunde_Id
Set EmailRecSet = db.OpenRecordset(strSQL)

Do Until EmailRecSet.EOF
  CurEmail1 = EmailRecSet.Fields.Item(0).Value
  MsgBox CurEmail1
  EmailRecSet.MoveNext
Loop

'Close before setting to nothing or you get an error (cant close nothing)
EmailRecSet.Close
Set EmailRecSet = Nothing

db.Close

End Function

It is not possible to call the correct name Me.Kunde-Id. The parser makes the Kunde-Id to Kunde - Id whith spaces. Me.Kunde_Id is not the correct name.
Avatar billede klk Nybegynder
23. december 2007 - 12:04 #10
Me.[Kunde-Id] doesn't help.
Avatar billede terry Ekspert
23. december 2007 - 18:12 #11
There is s distinct differnce between _ and -

If you use _ then there is no need to put the name in [] but if you use - it will be necessary.

What is the name of the field in the table?
If its Kunde-Id then use [Kunde-Id]. If the combo name
is Kunde_Id then it shouldnt be necessary to use [].
Avatar billede terry Ekspert
23. december 2007 - 18:13 #12
If you still hav eproblems is it possible to see the dB with the correct field names etc.?
Avatar billede klk Nybegynder
24. december 2007 - 10:07 #13
No it is not possible -sorry. If we can fix it in the sample database, it should work in the real one.

Regarding:
Tbl_Kunde, field Kunde-Id
And
The Kunde-Id field in the form

All names are correct and one to one. I havn't changed anything around these fields.

It should also fail with Me.[Kunde-id] in your database. Otherwise I should send the changed sample database. Is that nessesary?

I think I will logout for the Christmas.
Avatar billede terry Ekspert
24. december 2007 - 10:36 #14
But the sample isnt the same as the one in the question.

From original question
strSQL = "Select Email1 From Tbl_Advisering Where Advi_Id = 103"

Advi_Id has _

in the sample dB.
Avatar billede terry Ekspert
24. december 2007 - 10:37 #15
in the sample dB..
strSQL = "Select Info1 From Tbl_Advisering Where Advi-Id = 118"

Advi-Id has -
Avatar billede terry Ekspert
24. december 2007 - 10:37 #16
Anyway have a good Xmas
Avatar billede klk Nybegynder
27. december 2007 - 20:32 #17
Thank you. Same to you.

Yes I see your point. It is indead confusing. "Select Info1 From Tbl_Advisering Where [Advi-Id] = " & Me.Advi_Id was just unuseable sample code to understand the syntax.

I want to use the select statement to pick up the email address in the Tbl_Kunde for a specific Customer. Then I can use this email adress in the command:
DoCmd.SendObject .... in the calling SUB.

My problem is that:
strSQL = "Select E-mail1 From Tbl_Kunde Where [Kunde-Id] = " & Me.Kunde_Id
Does not work in the sample database. It should!

Please substitude the code from 23/12 12:04 in the sample database.

Otherwise ask me to send the changed sample database with the line:
strSQL = "Select E-mail1 From Tbl_Kunde Where [Kunde-Id] = " & Me.Kunde_Id
Avatar billede terry Ekspert
28. december 2007 - 09:43 #18
the easiest way to see if the select statement works is to copy it into a query.
If you set a breakpoint on the line following
strSQL = "Select E-mail1 From Tbl_Kunde Where [Kunde-Id] = " & Me.Kunde_Id

then you can get hold of the contents of strSQL in the debug window (CTRL+G)

Then write
?strSQL
followed by return.

You can now copy the actual SQL into a query where it is easier to see the actual problem.

Actually one problem is quite easy to see without doing that.
Your field names are not very good, you shouldnt use - , if you do yo should put the field in [] so that Access knows this is the name of a field.

Select [E-mail1]....

If you use Select E-mail1 then Access thinks it is this

Select [E]-[mail1]

which is two fields (remember when you were prompted for two paramaters?) and it then tries to subtract the two fields from each other which it cant because it cant find the fields. But if you entered two numbers when you get prompted you might see that it does that.

So you need to look carefully at all the table and field names and if possible only use
A to Z and 0 to 9

eMail1 for example
Avatar billede klk Nybegynder
28. december 2007 - 22:05 #19
Solution (løsning):
strSQL = "Select [E-mail1] From Tbl_Kunde Where [Kunde-Id] = " & Me.Kunde_Id

With this syntax CurEmail1 contain  the correct email address.

Thank you Terry. I'm ready to close the case...
Avatar billede terry Ekspert
29. december 2007 - 10:21 #20
great to see you got it working.
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