Fill in textbox based on value in different textbox

yschuerm

New Member
Joined
Jun 18, 2015
Messages
14
I have got an article Database in which I want to add new articles.
In the input form I need to fill in some specifics about the article.

What I want is the following. In case the article nr is known, the article description needs to be filled in automatically in different textbox. In case the article nr is not known, the focus needs to go to the article description textbox, so I can fill it in. This needs to happen on exit of the article nr textfield.

I tried somethings, but it either works on the first article in the table or it works on all articles but freezes when article nr is not known.

The code I currently have is the following :

Code:
Private Sub txtNewCompArtNr_Exit(Cancel As Integer)

 Dim db As Database
 Dim rs As Recordset

 Set db = CurrentDb
 'Set rs = db.OpenRecordset("SELECT CompetitorArtNr,BernerArtNr, [Competitor Art Description] FROM Competitors")
 Set rs = db.OpenRecordset("Competitors")

 rs.MoveFirst
 Do Until rs.EOF
 If rs.Fields("CompetitorArtNr").Value = Me.txtNewCompArtNr.Value Then
 
 Me.txtNewCompArtDes.Value = rs.Fields("Competitor Art Description").Value
 Me.txtBernArt.Value = rs.Fields("BernerArtNr").Value
 Me.cbSource.SetFocus
 
 Exit Sub
 Else
 rs.MoveNext
 Exit Do
 End If
rs.MoveNext
 Loop

 rs.Close
 db.Close


End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I'm not understanding your issue completely. It's hard to equate "article nr" with anything in your code sample. What doesn't look right is that if this is not true
If rs.Fields("CompetitorArtNr").Value = Me.txtNewCompArtNr.Value Then
you are moving to the next record, exiting the loop, then closing the recordset. I have no idea why you'd choose to close the db after that.
I can only suggest that after you close the recordset, move the focus to the desired control and don't close the db.
 
Upvote 0

Forum statistics

Threads
1,221,837
Messages
6,162,282
Members
451,759
Latest member
damav78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top