Option Compare Database
Option Explicit
Const Cm2Twips = 566.93
'pass args directly by peiyezhu
Sub CreateTblParts()
Dim dbs As DAO.Database
Dim strSql As String
Set dbs = CurrentDb
dbs.Execute "DROP TABLE tblTransactions"
dbs.Execute "DROP TABLE tblParts"
dbs.Execute "CREATE TABLE tblParts (MastNumber TEXT PRIMARY KEY, Description TEXT,CurrentQuantity DOUBLE )"
strSql = "CREATE TABLE tblTransactions (TransId INTEGER PRIMARY KEY, MastNumber TEXT , Quantity DOUBLE,CONSTRAINT FKTransMasterNumber FOREIGN KEY (MastNumber) REFERENCES tblParts) "
dbs.Execute strSql
dbs.Close
End Sub
Sub InsertValues4tblParts()
Dim dbs As DAO.Database
Dim strSql As String
Set dbs = CurrentDb
strSql = "INSERT INTO tblParts(MastNumber,Description,CurrentQuantity) "
strSql = strSql & " VALUES ("
strSql = strSql & "'P0001',"
strSql = strSql & "'Part1',"
strSql = strSql & "100"
strSql = strSql & ")"
dbs.Execute strSql
strSql = "INSERT INTO tblParts(MastNumber,Description,CurrentQuantity) "
strSql = strSql & " VALUES ("
strSql = strSql & "'P0002',"
strSql = strSql & "'Part2',"
strSql = strSql & "300"
strSql = strSql & ")"
dbs.Execute strSql
End Sub
Sub InsertValues4tblTransactions()
Dim dbs As DAO.Database
Dim strSql As String
Set dbs = CurrentDb
strSql = "INSERT INTO tblTransactions(TransId, MastNumber, Quantity) "
strSql = strSql & " VALUES ("
strSql = strSql & "1,"
strSql = strSql & "'P0001',"
strSql = strSql & "100"
strSql = strSql & ")"
dbs.Execute strSql
strSql = "INSERT INTO tblTransactions(TransId,MastNumber,Quantity) "
strSql = strSql & " VALUES ("
strSql = strSql & "2,"
strSql = strSql & "'P0002',"
strSql = strSql & "300"
strSql = strSql & ")"
dbs.Execute strSql
End Sub
Sub CreateFrmParts()
Dim f As Form
Set f = Application.CreateForm
'Set f = Screen.ActiveForm
Dim lst As ListBox
Set lst = Application.CreateControl(f.Name, acListBox, acDetail, , , 1 * Cm2Twips, 0.5 * Cm2Twips, 3 * Cm2Twips, 0.5 * Cm2Twips)
'Set lst = Screen.ActiveControl
With lst
.Left = 0.1 * Cm2Twips
.Height = 5 * Cm2Twips
.Width = 8 * Cm2Twips
.RowSource = "SELECT MastNumber,Description,CurrentQuantity FROM tblParts"
.ColumnCount = 3
.ColumnHeads = True
.ColumnWidths = "2cm;2.6cm;0.6cm"
.Name = "lstMastNumber"
End With
Dim cmd As CommandButton
Set cmd = Application.CreateControl(f.Name, acCommandButton, acDetail, , , lst.Left + lst.Width + 1 * Cm2Twips, lst.Top, 2 * Cm2Twips, 1 * Cm2Twips)
cmd.Name = "cmdSignOut"
cmd.OnClick = "=fSignOut()"
cmd.Caption = "SignOut"
Dim strName As String
strName = f.Name
DoCmd.Save acForm, f.Name
DoCmd.Restore
DoCmd.Close acForm, f.Name
DoCmd.Rename "Parts", acForm, strName
DoCmd.OpenForm "Parts"
End Sub
Sub CreateFrmSignOut()
Dim f As Form
' DoCmd.Close acForm, "SignOut"
' DoCmd.DeleteObject acForm, "SignOut"
Set f = Application.CreateForm
f.OnLoad = "=fSignOutOnLoad()"
'txtPartNumber
Dim dblLeft, dblWidth, dblHeight, dblTop
Dim txt As TextBox
Dim lbl As Label
Set txt = Application.CreateControl(f.Name, acTextBox, acDetail, , , 3 * Cm2Twips, 0.5 * Cm2Twips, 2 * Cm2Twips, 0.5 * Cm2Twips)
txt.Name = "txtMastNumber"
dblLeft = txt.Left
dblWidth = txt.Width
dblHeight = txt.Height
dblTop = txt.Top
Set lbl = Application.CreateControl(f.Name, acLabel, acDetail, txt.Name, , dblLeft - dblWidth - 0.1 * Cm2Twips, dblTop, dblWidth, dblHeight)
lbl.Name = "lblMastNumber"
lbl.Caption = "MastNumber"
'txtDate
dblLeft = txt.Left
dblWidth = txt.Width
dblHeight = txt.Height
dblTop = txt.Top + txt.Height + 0.1 * Cm2Twips
Set txt = Application.CreateControl(f.Name, acTextBox, acDetail, , , dblLeft, dblTop, dblWidth, dblHeight)
txt.Name = "txtDate"
dblLeft = dblLeft - dblWidth - 0.1 * Cm2Twips
dblWidth = txt.Width
dblHeight = txt.Height
dblTop = txt.Top
Set lbl = Application.CreateControl(f.Name, acLabel, acDetail, txt.Name, , dblLeft, dblTop, dblWidth, dblHeight)
lbl.Name = "lblTransDate"
lbl.Caption = "TransDate"
'quantity
dblLeft = txt.Left
dblWidth = txt.Width
dblHeight = txt.Height
dblTop = txt.Top + txt.Height + 0.1 * Cm2Twips
Set txt = Application.CreateControl(f.Name, acTextBox, acDetail, , , dblLeft, dblTop, dblWidth, dblHeight)
txt.Name = "txtQantity"
dblLeft = dblLeft - dblWidth - 0.1 * Cm2Twips
dblWidth = txt.Width
dblHeight = txt.Height
dblTop = txt.Top
Set lbl = Application.CreateControl(f.Name, acLabel, acDetail, txt.Name, , dblLeft, dblTop, dblWidth, dblHeight)
lbl.Name = "lblQantity"
lbl.Caption = "Qantity"
Dim cmd As CommandButton
Set cmd = Application.CreateControl(f.Name, acCommandButton, acDetail, , , txt.Left + txt.Width + 1 * Cm2Twips, txt.Top, 2 * Cm2Twips, 1 * Cm2Twips)
cmd.Name = "cmdOK"
'cmd.OnClick = "=fSignOutOK()" 'need further development
cmd.Caption = "OK"
Dim strName As String
strName = f.Name
DoCmd.Save acForm, f.Name
DoCmd.Restore
DoCmd.Close acForm, f.Name
DoCmd.Rename "SignOut", acForm, strName
DoCmd.OpenForm "SignOut"
End Sub
Function fSignOut()
If Nz(Forms("Parts").Controls("lstMastNumber")) = "" Then
MsgBox "Select a MastNumber First!"
Else
DoCmd.OpenForm "SignOut"
End If
End Function
Function fSignOutOnLoad()
With Forms("SignOut")
.Controls("txtMastNumber") = Forms("Parts").Controls("lstMastNumber") 'pass args directly
.Controls("txtDate") = Format(Now, "dd-mmm-yyyy")
.Controls("txtQantity").SetFocus
End With
End Function