Funktion mit Recordsetfeldübergabe

J

Jörg Harnisch

Hallo NG

ich habe eine Funktion geschrieben und möchte in einem Formular einen Wert
aus einem Recordset erhalten.
Ich habe in der Tabelle die ich mit dem RS öffne verschiedene Felder und je
nach dem welches ich übergebe, möchte ich den Inhalt
zurückbekommen. Nun ist mein Problem, dass die Funktion mir einen String
erzeugt, der dann zurückgegeben wird, aber nicht der Inhalt des RS.
Bitte um Hilfe.

Function LeWertKundDet(Was)
Dim dbs2 As Database
Dim rs2 As Recordset
Set dbs2 = CurrentDb
If IsNull(Forms!KundenFor!ID) = False Then
Set rs2 = dbs2.OpenRecordset("select * from Kunden_KontaktTab where
VWert =" & Forms!KundenFor!ID & " order by EDat")
If rs2.RecordCount > 0 Then
rs2.MoveLast
Was = rs2! & Was
LeWertKundDet = Was
End If
rs2.Close
End If
Set dbs2 = Nothing
End Function

Wenn ich jetzt ein Control erstelle mit dem Inhalt =LeWertKundDet(K_Tel),
dann bekomme ich als Wert "rs2!K_Tel" zurück, aber nicht den Inhalt. Ich
hoffe, man kann mich verstehen ;)

Danke
Jörg Harnisch
 
W

Wolfgang Kais

Hallo Jörg.

Dies ist eine englische Newsgroup, daher weiter in Englisch.

Jörg Harnisch said:
Hello NG.

I have written a function and I want to receive a value from a
recordset in a form. The table I base the recorset in has several
fields, and depending on what field I pass as parameter value, I
want to reveive the content of that field.
The problem is, that the function creates and returns a string but
not the content of the recordset. Please help.

Function LeWertKundDet(Was)
Dim dbs2 As Database
Dim rs2 As Recordset
Set dbs2 = CurrentDb
If IsNull(Forms!KundenFor!ID) = False Then
Set rs2 = dbs2.OpenRecordset("select * from " & _
"Kunden_KontaktTab where VWert =" & Forms!KundenFor!ID & _
" order by EDat")
If rs2.RecordCount > 0 Then
rs2.MoveLast
Was = rs2! & Was
LeWertKundDet = Was
End If
rs2.Close
End If
Set dbs2 = Nothing
End Function

Now, when I create a control with content =LeWertKundDet(K_Tel),
I get a value of "rs2!K_Tel", but not the content. I hope you can
understand me.

First: This group is named modulesdaovba.ado but this is not an ado
question, you are using only dao.
I guess that "Was" (what) is a field name, so how about this:

Function LeWertKundDet(Was)
Dim strSQL as String
LeWertKundDet = Null
If Not IsNull(Forms!KundenFor!ID) Then
strSQL = "Select Top 1 " & Was & " from Kunden_KontaktTab " & _
"Where VWert = " & Forms!KundenFor!ID & " Order By EDat Desc"
With CurrentDb.OpenRecordset(strSQL)
If Not .EOF Then LeWertKundDet = .Fields(0).Value
.Close
End With
End If
End Function

If you only want to change you function:
delete the "Was = rs2! & Was" line and make the next line:
LeWertKundDet = rs2(Was)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top