1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
Sub EnterItemID()
'
' EnterItemID Macro
'
' to be sure first clean all data
Cells.Select
Range("B2:Z65536").Select
Selection.Clear
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
' Item Id
Dim Title0
Dim itemnr As String
Dim Msg, Style, Title, Response, MyString
Msg = "Item ID?"
Title0 = "Enter Reel/Item"
itemnr = InputBox(Msg, Title0)
' two options: either only standard BIN either also with Machine input output machines BIN
Msg = "Click YES **L I V E** (Or NO for TEST)" ' Define the message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define the button.
Title = "Select System (Live or Test)" ' Define the title.
' Show the next message (not used at the moment).
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' For the YES option
MyString = "Yes" ' Set the action
Else ' For the NO option
MyString = "No" ' Set the action
End If
'Declare variables'
Dim objMyConn As ADODB.Connection
Dim objMyCmd As ADODB.Command
Dim objMyRecordset As ADODB.Recordset
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection'
If MyString = "Yes" Then ' For the YES option read LIVE DB
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=*********;Initial Catalogue=*****;User ID=*****;Password=*****;"
Else ' For the NO option read TEST DB
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=*****;Initial Catalogue=*****;User ID=*****;Password=*****;"
End If
objMyConn.Open
'Set and Excecute SQL Command'
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandText = "SELECT zmh.LotNo, zlnc.String02, pr.ProductNo, wh.Location, zmh.Quantity, zmh.UomCode, zmh.InventoryStatus, " & _
"zmh.ReasonCode, zmh.CreatedOn, zmh.CreatedBy, zmh.MovementType, zmh.Module, zmh.ActionType, " & _
"zmh.Consignment " & _
"FROM [*****].[dbo].[Z_MOVEMENT_HISTORY] AS zmh INNER JOIN " & _
"[*****].[dbo].[WAREHOUSE_LOCATION] AS wh ON wh.ID = zmh.WarehouseLocationID INNER JOIN " & _
"[*****].[dbo].[PRODUCT] AS pr ON zmh.ProductID = pr.ID INNER JOIN " & _
"[*****].[dbo].[z_lot_no_characteristic] AS zlnc ON zlnc.LotNo = zmh.LotNo " & _
"WHERE zmh.lotno = '" & itemnr & "' "
' Previously used but can select too much --> "WHERE zmh.LotNo like '%" & itemnr & "%' "
objMyCmd.CommandType = adCmdText
'Open Recordset'
Set objMyRecordset.Source = objMyCmd
objMyRecordset.Open
Range("b2").Select
ActiveSheet.Range("b2").CopyFromRecordset objMyRecordset
End Sub |
Partager