开发者

LightSwitch: On New Row, Pull in Parent Info

I have a database with two tables in it: People and Logs.

I have created a Details page that displays Person information and additionally includes the associated logs records. All of this has just been done by adding a standard details page - not custom coding, etc.

When a user clicks on the green + to add a row to the Logs for that person I want to autopopulate a few of the columns. To do this I've opened the Log class and added the following:

Private Sub Log_Created()
     Me.Create_Date = Date.Now
End Sub

This works great. But I also want to pull in the card number associated with the person. In the end my code should look something like (pseudo):

Private Sub Log_Created()
     Me.Create_Date = Date.Now
     M开发者_如何学Ce.CardNumber = SelectedPerson.CardNumber
End Sub

However, I'm not sure exactly how to accomplish this in code?


To avoid denormalized data (http://en.wikipedia.org/wiki/Database_normalization), I'd avoid setting up your data model like this. I'd take the approach of

Person (Class)
 - Name
 - Address
 - ...
 - Cards (Collection of Card)

Card (Class)
 - CardNumber
 - Person 
 - Logs (Collection of Log)

Log (Class)
 - CreateDate
 - Card 

This would allow for multiple cards per person. If you really want to do what you're trying to do, you're on the right track. Make sure that you have a relationship between Person and Log (one person to many logs), and your code would be

Private Sub Log_Created()
     Me.Create_Date = Date.Now
     Me.CardNumber = Person.CardNumber
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜