21. december 2007 - 09:55Der 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)
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
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.
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 [].
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
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
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.