Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > กำหนด Format Autonumber ใน Access 2003 แล้วแสดงข้อมูลใน VB.net


 

[.NET] กำหนด Format Autonumber ใน Access 2003 แล้วแสดงข้อมูลใน VB.net

 
Topic : 044289



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter


คือแบบว่า กำหนด Format autonumber ใน access เป็น 0000 (เพราะมันเป็น autonumber เลยใส่ตัวอักษรไม่ได้...รึเปล่าคะ?) ใน access ก็ขึ้นนะ เช่น 0001, 0002, 0003 แต่พอไปเรียกมาแสดงใน VB.net ขึ้นแค่ 1, 2, 3

พอจะมีวิธีให้นำศูนย์ข้างหน้ามาแสดงมั้ยคะ?

แล้วถ้าจะให้มีตัวอักษรด้วยแบบ E0001, E0002, E0003 จะมีวิธีไหนหรอคะ??



Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-06-21 13:44:46 By : akura View : 8263 Reply : 11
 

 

No. 1



โพสกระทู้ ( 1,603 )
บทความ ( 1 )



สถานะออฟไลน์


ถ้าเป็น ACCESS ให้เอามาเขียนที่ .NET ค่ะ
เพราะ Access เน้นใช้ Macro ไม่ค่อยมี build-in function ให้ใช้
Date : 2010-06-21 13:50:25 By : blurEyes
 

 

No. 2



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

คือเขียน code autonumber ใน .net หรอคะ??
Date : 2010-06-21 13:59:36 By : akura
 

 

No. 3

Guest


มีตัว e แล้วมันจะเป็น number ได้ไง

------------------------------------------------

ให้เพิ่มอีก field ไว้เก็บรหัสดีกว่า โดยอ้างอิงจาก autonumber

ส่วนจะเก็บอย่างไร ต้องอาศัยโค้ดของ .net
Date : 2010-06-21 14:20:19 By : tungman
 

 

No. 4



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

นี่แหละคะประเด็น =w=

ไม่รู้ทำไง พอดีเป็นมือใหม่ VB เพิ่งจะจับไม่ถึงเดือน แฮะๆ


ขอความกรุณาด้วยค่ะ > <
Date : 2010-06-21 15:30:43 By : akura
 

 

No. 5



โพสกระทู้ ( 249 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ถ้าต้องการเลขรันแบบ E0001, E0002, E0003
ก็ต้องไป set data type ใน access เป็น text
แล้วเขียนโค้ดรัน number เองครับ ตัวอย่าง
ในบอร์ดน่าจะมีครับ
Date : 2010-06-21 16:31:36 By : tee
 

 

No. 6



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

ขอบคุณค่ะ เขียนแล้วเหมือนกัน แต่ไม่รู้ว่าถูก syntax ป่าว เพราะเขียนเองคะ เหอๆ

Dim conDB As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataHead.mdb"
Dim con As New OleDbConnection(conDB)
con.Open()

Dim query As String
query = "SELECT CodeAbsent "
query &= "FROM tblAbsent ORDER BY CodeAbsent deas"

Val(query)
Dim sum As Integer
sum = query + 1
Str(query)
Date : 2010-06-21 16:39:39 By : akura
 

 

No. 7



โพสกระทู้ ( 1,603 )
บทความ ( 1 )



สถานะออฟไลน์


ส่วนนี้เป็น
DATA ACCESS LAYER ของ MS ACCESS ค่ะ
สร้าง CLASS มาใหม่แล้ว copy แปะ
Code (VB.NET)
001.Imports System
002.Imports System.Data
003.Imports System.Data.OleDb
004.Imports System.IO
005.Imports System.Text
006.Imports System.Collections
007.Imports System.Collections.Generic
008. 
009.Imports System.Runtime.CompilerServices
010. 
011.'' Simple MsAccess Data Access Layer
012.'' Develop by Proud PNK.R. U.
013.'' Date 21-06-2010
014.'' Purpose : Provide neccessary method for manipulate Ms Access database through OleDB.
015.'' Some suggestion feature : Can be connect ACCESS 2007 (*.ACCDB) and common ACCESS (*.MDB) with auto detection.
016.''                           But i not yet to test with .MDB ,'coz my machine no have it. ~> <~
017. 
018.Namespace OleDbWithMSAccess
019. 
020.    Public Enum MSAccessVersionInfo
021.        AccessCommon = 1
022.        Access2007 = 2
023. 
024.    End Enum
025. 
026.    Public Class MSAccessConnectionArgs
027. 
028.        Private Const constAccess2007ConnectionStringFrame = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" _
029.            & "Persist Security Info=False;"
030.        Private Const constAccess2007ConnectionStringFrameWithUserNameAndPassword = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" _
031.            & "Jet OLEDB:Database Password={1};"
032. 
033.        Private Const constAccessCommonConnectionStringFrame = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" _
034.            & "Persist Security Info=False;"
035.        Private Const constAccessCommonConnectionStringFrameWithUserNameAndPassword = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" _
036.            & "User Id=admin;Password={1};"
037. 
038.        Private Const constMsAccessCommandParameterPrefix As String = "@"
039. 
040.        Private _VersionInfo As MSAccessVersionInfo = MSAccessVersionInfo.AccessCommon
041.        Private _DataBaseFile As String = String.Empty
042.        Private _HasUserName As Boolean = False
043.        'Private _UserName As String = String.Empty
044.        Private _Password As String = String.Empty
045. 
046.        Public Property VersionInfo() As MSAccessVersionInfo
047.            Get
048.                Return _VersionInfo
049.            End Get
050.            Set(ByVal value As MSAccessVersionInfo)
051.                _VersionInfo = value
052.            End Set
053.        End Property
054.        Public Property DataBaseFileName() As String
055.            Get
056.                Return _DataBaseFile
057.            End Get
058.            Set(ByVal value As String)
059.                _DataBaseFile = value
060.                If ((_UserName.Length = 0) And (_Password.Length = 0)) Then
061.                    _HasUserName = False
062.                Else
063.                    _HasUserName = True
064.                End If
065. 
066.                'Auto detect version from file extension
067.                Dim fileExtension As String = _DataBaseFile.Substring(_DataBaseFile.LastIndexOf("."c) + 1).ToUpper()
068.                If (fileExtension.Equals("ACCDB")) Then
069.                    _VersionInfo = MSAccessVersionInfo.Access2007
070.                Else
071.                    _VersionInfo = MSAccessVersionInfo.AccessCommon
072.                End If
073. 
074.            End Set
075.        End Property
076.        'Public Property UserName() As String
077.        '    Get
078.        '        Return _UserName
079.        '    End Get
080.        '    Set(ByVal value As String)
081.        '        _UserName = value
082.        '        If ((_UserName.Length = 0) And (_Password.Length = 0)) Then
083.        '            _HasUserName = False
084.        '        Else
085.        '            _HasUserName = True
086.        '        End If
087. 
088.        '    End Set
089.        'End Property
090.        Public Property Password() As String
091.            Get
092.                Return _Password
093.            End Get
094.            Set(ByVal value As String)
095.                _Password = value
096.                If (_Password.Length = 0) Then
097.                    _HasUserName = False
098.                Else
099.                    _HasUserName = True
100.                End If
101.            End Set
102.        End Property
103.        Public Property HasUserNameAndPassword() As Boolean
104.            Get
105.                Return _HasUserName
106.            End Get
107.            Set(ByVal value As Boolean)
108.                _HasUserName = value
109.            End Set
110.        End Property
111. 
112.        Public Sub New()
113.        End Sub
114. 
115.        Public Sub New(ByVal argVersionInfo As MSAccessVersionInfo)
116.            Me.VersionInfo = argVersionInfo
117.        End Sub
118.        Public Sub New(ByVal argVersionInfo As MSAccessVersionInfo, ByVal argDatabaseFileName As String)
119.            Me.VersionInfo = argVersionInfo
120.            Me.DataBaseFileName = argDatabaseFileName
121.        End Sub
122. 
123.        Public Function IsReadyToConnect() As Boolean
124.            Return (Me.DataBaseFileName.Length = 0)
125.        End Function
126. 
127.        Public Function GetConnectionString() As String
128.            Dim retStr As String = String.Empty
129.            Dim ConnStr As String = String.Empty
130. 
131.            If (Me.VersionInfo = MSAccessVersionInfo.AccessCommon) Then
132. 
133.                If (Me.HasUserNameAndPassword) Then
134.                    retStr = String.Format(constAccessCommonConnectionStringFrameWithUserNameAndPassword _
135.                                           , Me.DataBaseFileName, Me.Password)
136.                Else
137.                    retStr = String.Format(constAccessCommonConnectionStringFrame _
138.                                           , Me.DataBaseFileName)
139.                End If
140.            Else
141. 
142.                If (Me.HasUserNameAndPassword) Then
143.                    retStr = String.Format(constAccess2007ConnectionStringFrameWithUserNameAndPassword _
144.                                           , Me.DataBaseFileName, Me.Password)
145.                Else
146.                    retStr = String.Format(constAccess2007ConnectionStringFrame _
147.                                           , Me.DataBaseFileName)
148.                End If
149. 
150.            End If
151. 
152.            Return retStr
153.        End Function
154. 
155.    End Class
156. 
157.    Public Class MsAccessSQLCommandArgs
158. 
159.        Public ConnectionArgs As MSAccessConnectionArgs = New MSAccessConnectionArgs()
160.        ''' <summary>
161.        ''' Command String with SQL Statement [If you would like to use 'LIKE'
162.        ''' operator only wildcard charactor '%' works for any charactor]
163.        ''' </summary>
164.        ''' <remarks></remarks>
165.        '''
166.        Public CommandStr As String = String.Empty
167.        Public CommandParameters As Hashtable = New Hashtable()
168. 
169.        Public Sub New()
170. 
171.        End Sub
172. 
173.        Public Sub ClearAll()
174.            CommandStr = String.Empty
175.            CommandParameters = New Hashtable()
176.        End Sub
177. 
178.        Public Sub ClearParameters()
179.            CommandParameters = New Hashtable()
180.        End Sub
181. 
182.        Public Sub AddParameter(ByVal argKey As String, ByVal argValue As Object)
183.            CommandParameters.Add(argKey, argValue)
184.        End Sub
185. 
186.    End Class
187. 
188.    Module MsAccessExtension
189. 
190.        Private Const constMsAccessCommandParameterPrefix As String = "@"
191. 
192.        <Extension()> _
193.        Public Sub ExecuteCommand(ByVal e As MsAccessSQLCommandArgs)
194.            Using myConnection As OleDbConnection _
195.                = New OleDbConnection(e.ConnectionArgs.GetConnectionString())
196. 
197.                Dim myCommand As OleDbCommand _
198.                    = New OleDbCommand(e.CommandStr, myConnection)
199. 
200.                myCommand.CommandType = CommandType.Text
201. 
202.                If (e.CommandParameters.Count > 0) Then
203. 
204.                    For Each keyStr As String In e.CommandParameters.Keys
205.                        Dim tempKey As String = keyStr
206.                        If (Not keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix)) Then
207.                            tempKey = constMsAccessCommandParameterPrefix & tempKey
208.                        End If
209.                        myCommand.Parameters.AddWithValue(tempKey, e.CommandParameters(keyStr))
210.                    Next
211. 
212.                End If
213. 
214.                Try
215.                    myConnection.Open()
216.                    myCommand.ExecuteNonQuery()
217.                Catch ex As Exception
218.                    Throw New Exception(ex.Message)
219.                Finally
220.                    If Not (myConnection Is Nothing) Then
221.                        myConnection.Close()
222.                    End If
223.                End Try
224. 
225.            End Using
226.        End Sub
227. 
228.        <Extension()> _
229.        Public Function ExecuteCommandScalar(ByVal e As MsAccessSQLCommandArgs) As Object
230. 
231.            Dim oRet As Object = Nothing
232. 
233.            Using myConnection As OleDbConnection _
234.                = New OleDbConnection(e.ConnectionArgs.GetConnectionString())
235. 
236.                Dim myCommand As OleDbCommand _
237.                    = New OleDbCommand(e.CommandStr, myConnection)
238. 
239.                myCommand.CommandType = CommandType.Text
240. 
241.                If (e.CommandParameters.Count > 0) Then
242. 
243.                    For Each keyStr As String In e.CommandParameters.Keys
244.                        Dim tempKey As String = keyStr
245.                        If (Not keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix)) Then
246.                            tempKey = constMsAccessCommandParameterPrefix & tempKey
247.                        End If
248.                        myCommand.Parameters.AddWithValue(tempKey, e.CommandParameters(keyStr))
249.                    Next
250. 
251.                End If
252. 
253.                Try
254.                    myConnection.Open()
255.                    oRet = myCommand.ExecuteScalar()
256.                Catch ex As Exception
257.                    Throw New Exception(ex.Message)
258.                Finally
259.                    If Not (myConnection Is Nothing) Then
260.                        myConnection.Close()
261.                    End If
262.                End Try
263. 
264.                Return oRet
265. 
266.            End Using
267.        End Function
268. 
269.        ''' <summary>
270.        ''' Return DataTable from MsAccessSQLCommandArgs
271.        ''' </summary>
272.        ''' <param name="e"></param>
273.        ''' <returns></returns>
274.        ''' <remarks></remarks>
275.        <Extension()> _
276.        Public Function GetDataTable(ByVal e As MsAccessSQLCommandArgs) As DataTable
277. 
278.            Dim dtRet As DataTable = New DataTable()
279. 
280.            Using myConnection As OleDbConnection _
281.                = New OleDbConnection(e.ConnectionArgs.GetConnectionString())
282. 
283.                Dim myCommand As OleDbCommand _
284.                    = New OleDbCommand(e.CommandStr, myConnection)
285. 
286.                myCommand.CommandType = CommandType.Text
287. 
288.                If (e.CommandParameters.Count > 0) Then
289. 
290.                    For Each keyStr As String In e.CommandParameters.Keys
291.                        Dim tempKey As String = keyStr
292.                        If (Not keyStr.Substring(0, 1).Equals(constMsAccessCommandParameterPrefix)) Then
293.                            tempKey = constMsAccessCommandParameterPrefix & tempKey
294.                        End If
295.                        myCommand.Parameters.AddWithValue(tempKey, e.CommandParameters(keyStr))
296.                    Next
297. 
298.                End If
299.                Dim myDataAdaptor As OleDbDataAdapter = New OleDbDataAdapter(myCommand)
300. 
301.                Try
302.                    myConnection.Open()
303.                    myDataAdaptor.Fill(dtRet)
304. 
305.                Catch ex As Exception
306.                    Throw New Exception(ex.Message)
307.                Finally
308.                    If Not (myConnection Is Nothing) Then
309.                        myConnection.Close()
310.                    End If
311.                End Try
312. 
313.                Return dtRet
314. 
315.            End Using
316.        End Function
317. 
318.    End Module
319. 
320.End Namespace

Date : 2010-06-22 13:01:00 By : blurEyes
 

 

No. 8



โพสกระทู้ ( 1,603 )
บทความ ( 1 )



สถานะออฟไลน์


ตอนใช้งาน ก็ประมาณนี้ค่ะ
Code (VB.NET)
01.Imports VB.OleDbWithMSAccess
02. 
03.Public Class Form1
04. 
05.    Private AppConnectionArg As MSAccessConnectionArgs = New MSAccessConnectionArgs()
06.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
07. 
08.        AppConnectionArg.VersionInfo = MSAccessVersionInfo.Access2007
09.        AppConnectionArg.DataBaseFileName = "D:\BIN\Northwind 2007.accdb"
10. 
11. 
12.        Dim cmd As MsAccessSQLCommandArgs = New MsAccessSQLCommandArgs()
13.        cmd.ConnectionArgs = AppConnectionArg
14.        cmd.CommandStr = "SELECT MAX(ID) FROM [Employees] "
15.        
16.        Dim Counter As Integer CInt(cmd.ExecuteCommandScalar())
17.        Counter += 1
18.         
19.        Dim IDStr As String = "E" & Counter.ToString("00000")
20. 
21.    End Sub
22. 
23.End Class

Date : 2010-06-22 13:05:45 By : blurEyes
 

 

No. 9



โพสกระทู้ ( 94 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

อยากเข้าใจโค้ดด้วย อันนี้จะรบกวนมากไปรึเปล่าคะ > <

ขอโทษด้วยนะคะ

แล้วก็ขอบคุณสำหรับโค้ดมากค่ะ จะลองดู
Date : 2010-06-22 13:22:44 By : akura
 

 

No. 10



โพสกระทู้ ( 1,603 )
บทความ ( 1 )



สถานะออฟไลน์


code พราวเขียนทดลองค่ะ ว่า method extension จะไปกันได้กับ vb หรือเปล่า
สงสัยตรงไหนก้อถามมาดีกว่าค่ะ เรียนแบบอุดมศึกษา ก้อจะประมาณนี้แหละค่ะ

มาดู code ตัวเองอีกที เอ๋อรับทานค่ะ
ต้องงี้

Code (VB.NET)
01.Imports VB.OleDbWithMSAccess
02. 
03.Public Class Form1
04. 
05.    Private AppConnectionArg As MSAccessConnectionArgs = New MSAccessConnectionArgs()
06.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
07. 
08.        AppConnectionArg.VersionInfo = MSAccessVersionInfo.Access2007
09.        AppConnectionArg.DataBaseFileName = "D:\BIN\Northwind 2007.accdb"      
10.         
11.        Dim IDStr As String = "E" & Counter.ToString("00000")
12. 
13.    End Sub
14. 
15.    Function NewAutoNumber (ByVal c As MSAccessConnectionArgs ) As String
16.         
17.        Dim Counter As Integer = 0
18.        Dim cmd As MsAccessSQLCommandArgs = New  MsAccessSQLCommandArgs()
19.        cmd.ConnectionArgs = c
20.        'Field ID มี Type เป็น TEXT นะคะ
21.        cmd.CommandStr = "SELECT MAX(ID) FROM [TableName] "
22.        Dim tempStr As String = cmd.ExecuteCommandScalar().ToString()
23.         
24.        Counter = CInt(tempStr.SubString(1)) 'ตัดตัวอักษรออก แล้วเปลี่ยนเป็น integer
25.        Counter += 1
26. 
27.        tempStr = "E" + Counter.ToString("000000")
28.       
29.        Return tempStr        
30. 
31.    End Function
32.     
33. 
34. 
35.End Class

Date : 2010-06-22 15:01:12 By : blurEyes
 

 

No. 11

Guest


ค่าใหม่ : เพิ่มค่า
รูปแบบ : \E0000
ใส่ดัชนี : ไม่ซ้ำกัน
Date : 2015-12-05 12:07:32 By : Arisa
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : กำหนด Format Autonumber ใน Access 2003 แล้วแสดงข้อมูลใน VB.net
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่