Option Explicit
Dim Conn As New ADODB.Connection
Dim rsItemType As New ADODB.Recordset
Dim rsBrandName As New ADODB.Recordset
Dim rsSupplier As New ADODB.Recordset
Dim BrandPictureFileName As String
Dim ItemPictureFileName As String
Private Sub Form_Load()
Dim sqlItemType As String
Dim sqlBrandName As String
Dim sqlSupplier As String
frmItemMaster.WindowState = 2
txtItem(0).MaxLength = 20
txtItem(1).MaxLength = 100
txtItem(2).MaxLength = 200
txtItem(5).MaxLength = 254
With Conn
If .State = adStateOpen Then .Close
.ConnectionString = strConn & ";Data Source =C:\stock2001.mdb"
.ConnectionTimeout = 90
.Open
End With
sqlItemType = "SELECT * FROM ItemType ORDER BY ItemTypeCode"
With rsItemType
If .State = adStateOpen Then .Close
.ActiveConnection = Conn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.Open sqlItemType
End With
sqlBrandName = "SELECT * FROM BrandName ORDER BY BrandCode"
With rsBrandName
If .State = adStateOpen Then .Close
.ActiveConnection = Conn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.Open sqlBrandName
End With
sqlSupplier = "SELECT SupplierCode,NameInThai FROM Supplier ORDER BY SupplierCode"
With rsSupplier
If .State = adStateOpen Then .Close
.ActiveConnection = Conn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.Open sqlSupplier
End With
Set dcbItemType.RowSource = rsItemType
dcbItemType.ListField = "ItemTypeName"
dcbItemType.BoundColumn = "ItemTypeCode"
Set dcbItem(0).RowSource = rsItemType
dcbItem(0).ListField = "ItemTypeName"
dcbItem(0).BoundColumn = "ItemTypeCode"
Set dcbItem(1).RowSource = rsBrandName
dcbItem(1).ListField = "BrandName"
dcbItem(1).BoundColumn = "BrandCode"
Set dcbItem(2).RowSource = rsSupplier
dcbItem(2).ListField = "NameInThai"
dcbItem(2).BoundColumn = "SupplierCode"
AdoItemMaster.Refresh
Call LockCtrl
End Sub
Private Sub AdoItemMaster_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
On Error GoTo PictureNotFound
With AdoItemMaster.Recordset
If (.BOF) Or (.EOF) Then Exit Sub
tmpItemModel = .Fields("ItemModel").Value 'ÍèÒ¹ÃØè¹ÊÔ¹¤éÒà¡çºäÇé·ÕèµÑÇá»Ã tmpItemModel
If .Fields("ItemStatus").Value = "0" Then
chkStatusItem.Value = Checked
ElseIf .Fields("ItemStatus").Value = "1" Then
chkStatusItem.Value = Unchecked
End If
End With
PictureNotFound:
If (Err.Number = 76) Or (Err.Number = 53) Then
BrandPictureFileName = ""
ItemPictureFileName = ""
imgBrandName.Picture = LoadPicture(BrandPictureFileName)
imgProduct.Picture = LoadPicture(ItemPictureFileName)
Else
With AdoItemMaster.Recordset
If (.Fields("BrandPictureFileName").Value = "") Or (IsNull(.Fields("BrandPictureFileName").Value)) Or (.Fields("ItemPictureFileName").Value = "") Or (IsNull(.Fields("ItemPictureFileName").Value)) Then
BrandPictureFileName = ""
ItemPictureFileName = ""
End If
If (.Fields("BrandPictureFileName").Value <> "") Or (.Fields("ItemPictureFileName").Value <> "") Then
BrandPictureFileName = IIf(IsNull(.Fields("BrandPictureFileName").Value), "", .Fields("BrandPictureFileName").Value)
imgBrandName.Picture = LoadPicture(BrandPictureFileName)
ItemPictureFileName = IIf(IsNull(.Fields("BrandPictureFileName").Value), "", .Fields("ItemPictureFileName").Value)
imgProduct.Picture = LoadPicture(ItemPictureFileName)
Else
imgBrandName.Picture = LoadPicture("")
imgProduct.Picture = LoadPicture("")
End If
End With
End If
With AdoItemMaster
.Caption = "¨Ó¹Ç¹ÊÔ¹¤éÒ : " & .Recordset.AbsolutePosition & " / " & .Recordset.RecordCount
End With
End Sub
Private Sub cmdAdd_Click()
Call UnLockCtrl
AdoItemMaster.Enabled = False
fraItemType.Enabled = False
cmdAdd.Enabled = False
cmdEdit.Enabled = False
cmdOK.Visible = True
cmdCancel.Visible = True
AdoItemMaster.Recordset.AddNew
txtItem(0).SetFocus
txtItem(0).Locked = False
txtItem(0).BackColor = &H80000009
chkStatusItem.Value = Unchecked
End Sub
Private Sub cmdEdit_Click()
Call UnLockCtrl
fraItemType.Enabled = False
cmdAdd.Enabled = False
cmdEdit.Enabled = False
cmdOK.Visible = True
cmdCancel.Visible = True
End Sub
Private Sub cmdBrowse1_Click()
BrandPictureFileName = ""
With cdlBrowse
.DialogTitle = "àÅ×Í¡ÃÙ»ÀÒ¾"
.CancelError = False
.Action = 1
BrandPictureFileName = .FileName
End With
If BrandPictureFileName <> "" Then
imgBrandName.Picture = LoadPicture(BrandPictureFileName)
End If
End Sub
Private Sub cmdBrowse2_Click()
ItemPictureFileName = ""
With cdlBrowse
.DialogTitle = "àÅ×Í¡ÃÙ»ÀÒ¾"
.CancelError = False
.Action = 1
ItemPictureFileName = .FileName
End With
If ItemPictureFileName <> "" Then
imgProduct.Picture = LoadPicture(ItemPictureFileName)
End If
End Sub
Private Sub cmdOK_Click()
On Error GoTo ItemModelDuplicate
Dim tmpShortName As String
Dim i As Integer
Dim sqlItem As String
Dim rsItemMaster As New ADODB.Recordset
If (txtItem(0).Text = "") Then
MsgBox "¡ÃسÒãÊèÃËÑÊÊÔ¹¤éÒ´éÇ !", vbOKOnly + vbCritical, "¢éͼԴ¾ÅÒ´"
txtItem(0).SetFocus
cmdAdd.Enabled = False
cmdEdit.Enabled = False
Exit Sub
End If
If (txtItem(1).Text = "") Or (txtItem(2).Text = "") Then
MsgBox "¡ÃسÒãÊèª×èÍÊÔ¹¤éÒ´éÇ !", vbOKOnly + vbCritical, "¢éͼԴ¾ÅÒ´"
txtItem(1).SetFocus
cmdAdd.Enabled = False
cmdEdit.Enabled = False
Exit Sub
End If
If (dcbItem(0).Text = "") Then
MsgBox "¡ÃسÒÃкػÃÐàÀ·ÊÔ¹¤éÒ´éÇ !", vbOKOnly + vbCritical, "¢éͼԴ¾ÅÒ´"
dcbItem(0).SetFocus
cmdAdd.Enabled = False
cmdEdit.Enabled = False
Exit Sub
End If
With AdoItemMaster.Recordset
If txtItem(0).Text <> "" Then
If chkStatusItem.Value = Checked Then
tmpShortName = RTrim(.Fields("ShortName").Value)
If Right$(tmpShortName, 15) <> "(¡àÅÔ¡¨Ó˹èÒÂ)" Then
.Fields("ShortName").Value = tmpShortName & " (¡àÅÔ¡¨Ó˹èÒÂ)"
End If
.Fields("ItemStatus").Value = "0"
ElseIf chkStatusItem.Value = Unchecked Then
tmpShortName = RTrim(.Fields("ShortName").Value)
tmpShortName = Replace(tmpShortName, "(¡àÅÔ¡¨Ó˹èÒÂ)", "")
.Fields("ShortName").Value = RTrim(tmpShortName)
.Fields("ItemStatus").Value = "1"
End If
End If
If (Val(Len(BrandPictureFileName)) > 254) Or (Val(Len(ItemPictureFileName)) > 254) Then
MsgBox "ÃÙ»ÀÒ¾·Õè¤Ø³àÅ×Í¡ à¡çºÍÂÙèã¹¾Ò¸·ÕèÂÒÇà¡Ô¹ä»!", vbOKOnly + vbExclamation, "¢éͼԴ¾ÅÒ´"
Exit Sub
End If
If (BrandPictureFileName <> "") Or (ItemPictureFileName <> "") Then
.Fields("BrandPictureFileName").Value = BrandPictureFileName
.Fields("ItemPictureFileName").Value = ItemPictureFileName
End If
If txtItem(4).Text = "" Then
.Fields("LowLimitToWarning").Value = 0
End If
.Fields("NumberInStock").Value = 0
.Fields("NumberInOrder").Value = 0
.Update
End With
Select Case AdoItemMaster.Recordset.Fields("ItemTypeCode").Value
Case "00"
sqlItem = "SELECT CPUModel FROM CPUDetail "
sqlItem = sqlItem & " WHERE CPUModel='" & Trim(txtItem(0).Text) & "'"
With rsItemMaster
If .State = adStateOpen Then .Close
.ActiveConnection = Conn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open sqlItem
If .RecordCount = 0 Then
.AddNew
.Fields("CPUModel").Value = Trim(txtItem(0).Text)
.Update
End If
End With
Case "01"
sqlItem = "SELECT MBModel FROM MainboardDetail "
sqlItem = sqlItem & " WHERE MBModel='" & Trim(txtItem(0).Text) & "'"
With rsItemMaster
If .State = adStateOpen Then .Close
.ActiveConnection = Conn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open sqlItem
If .RecordCount = 0 Then
.AddNew
.Fields("MBModel").Value = Trim(txtItem(0).Text)
.Update
End If
End With
End Select
tmpItemModel = txtItem(0).Text
Call LockCtrl
cmdAdd.Enabled = True
cmdEdit.Enabled = True
fraItemType.Enabled = True
AdoItemMaster.Enabled = True
cmdOK.Visible = False
cmdCancel.Visible = False
ItemModelDuplicate:
Select Case Err.Number
Case -2147467259
MsgBox "¤Ø³ãÊèÃËÑÊÊÔ¹¤éÒ«éÓ !", vbOKOnly + vbCritical, "¢éͼԴ¾ÅÒ´"
txtItem(0).SetFocus
txtItem(0).SelStart = 0
txtItem(0).SelLength = Len(txtItem(0).Text)
Exit Sub
End Select
End Sub
Private Sub cmdCancel_Click()
With AdoItemMaster
.Recordset.CancelUpdate
.Refresh
End With
cmdAdd.Enabled = True
cmdEdit.Enabled = True
fraItemType.Enabled = True
cmdOK.Visible = False
cmdCancel.Visible = False
Call LockCtrl
AdoItemMaster.Enabled = True
End Sub
Private Sub cmdItem_Click()
Dim sqlItem As String
If dcbItemType.BoundText = "" Then Exit Sub
sqlItem = "SELECT * FROM ItemMaster"
sqlItem = sqlItem & " WHERE (ItemTypeCode='" & dcbItemType.BoundText & "')"
If chkSalesOnly.Value = Checked Then
sqlItem = sqlItem & " AND (ItemStatus='1')"
End If
sqlItem = sqlItem & " ORDER BY ItemModel"
With AdoItemMaster
.RecordSource = sqlItem
.Refresh
If .Recordset.RecordCount = 0 Then
MsgBox "äÁèÁÕÃÒ¡ÒÃÊÔ¹¤éÒ " & dcbItemType.Text & " µÒÁ·Õè¤Ø³µéͧ¡ÒÃ", vbOKOnly + vbExclamation, "¼Å¡Òäé¹ËÒ"
sqlItem = "SELECT * FROM ItemMaster ORDER BY ItemModel"
.RecordSource = sqlItem
.Refresh
End If
End With
End Sub
Private Sub cmdDetail_Click()
If txtItem(0).Text = "" Then Exit Sub
Select Case AdoItemMaster.Recordset.Fields("ItemTypeCode").Value
Case "00"
frmCPU.Show vbModal
Case "01"
frmMainboard.Show vbModal
Case Else
MsgBox "äÁèÁÕÃÒÂÅÐàÍÕ´¢Í§ÊÔ¹¤éÒ µÒÁ·Õè¤Ø³µéͧ¡ÒÃ", vbOKOnly + vbInformation, "¼Å¡ÒõÃǨÊͺ"
End Select
End Sub
Private Sub LockCtrl()
Dim i As Integer
For i = 0 To txtItem.Count - 1
txtItem(i).Locked = True
txtItem(i).BackColor = &H8000000F 'ÊÕà·Ò
Next
For i = 0 To dcbItem.Count - 1
dcbItem(i).Locked = True
dcbItem(i).BackColor = &H8000000F 'ÊÕà·Ò
Next
cmdBrowse1.Enabled = False
cmdBrowse2.Enabled = False
chkStatusItem.Enabled = False
End Sub
Private Sub UnLockCtrl()
Dim i As Integer
For i = 1 To txtItem.Count - 1
txtItem(i).Locked = False
txtItem(i).BackColor = &H80000009 'ÊÕ¢ÒÇ
Next
For i = 0 To dcbItem.Count - 1
dcbItem(i).Locked = False
dcbItem(i).BackColor = &H80000009 'ÊÕ¢ÒÇ
Next
cmdBrowse1.Enabled = True
cmdBrowse2.Enabled = True
chkStatusItem.Enabled = True
End Sub
Private Sub cboSearch_KeyDown(KeyCode As Integer, Shift As Integer)
KeyCode = 0
End Sub
Private Sub cboSearch_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
Private Sub dcbItem_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
KeyCode = 0
End Sub
Private Sub dcbItem_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = 0
End Sub
Private Sub dcbItemType_KeyDown(KeyCode As Integer, Shift As Integer)
KeyCode = 0
End Sub
Private Sub dcbItemType_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
Private Sub txtItem_KeyPress(Index As Integer, KeyAscii As Integer)
Dim Character As String
Select Case Index
Case 3, 4
Character = "0123456789"
KeyAscii = Asc(Chr(KeyAscii))
If KeyAscii > 26 Then
If InStr(Character, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End If
End Select
End Sub