• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

Where's the error in the code?

I use a form to select multiple addresses for the print of envelopes. For the indication of a customers address, I use KIDNR, which contains numbers and letters. I use now

Private Sub Umschaltfläche6_Click()
Dim Element As Variant
Dim Bedingung As String
Dim KIDNR As Variant

If Liste.ItemsSelected.Count = 0 Then Exit Sub

For Each Element In Liste.ItemsSelected

KIDNR = Liste.ItemData(Element)

Bedingung = Bedingung & _
"KIDNR= " & KIDNR & " OR "

Next Element

Bedingung = Left(Bedingung, Len(Bedingung) - 4)


DoCmd.OpenReport "Couvert spezVersand", acPreview, , Bedingung
DoCmd.Close acForm, "Couvert spezial Mehrfachauswahl", acSaveNo
End Sub

But this does not properly work. After choosing the addresses, I get a small pop-up where I have to put in KIDNR manually, after have this done, the report opens and all the chosen addresses appearing.  

Thank you

1 Solution
Are you sure that KIDNR is a part of the reports datasource.
KongtaAuthor Commented:
I guess. I use partly the same query in the form & report. If I put KIDNR in the field, that appears on the wrong code, it after works and the report opens the right addresses. I guess the error has something to do with numbers and letters. I copied the code from a query where only numbers are used. As I know, it needs little changes when using letters. I've made the changes I thought have to be done, but does not work.
if KIDNR is s TEXT value, then chnage your code like this:

Bedingung = Bedingung & _
"KIDNR= '" & KIDNR & "' OR "

and also:

Bedingung = Left(Bedingung, Len(Bedingung) - 5)

A Text field MUST be enclosed in quote marks ('....') otherwise Access will treat the value like a Number, and you will get a TYPE MISMATCH error.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Does the query that feeds the report have a parameter in it?  Step through the data generation and you'll see it.
KongtaAuthor Commented:
Thanks to all

The answer from Arthur works. But only with the first code. When I used Bedingung=Left(Bedingung, Len(Bedingung) -5), it didn't react at all. Only for my better understanding; why you changed this code?

because I added an additional character:

"' OR "

which by my count is 5.

set a breakpoint on this line, and check what the real string is:

Bedingung=Left(Bedingung, Len(Bedingung) -5)

and adjust the (5) accordinly, to truncated the end of the string.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now